tbpgr_utils 0.0.127 → 0.0.128

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c4a2fe61b708e8b27e75368fd115028e037afd7
4
- data.tar.gz: d27e099beff9f6be727fd71eef614332cbc3d8f7
3
+ metadata.gz: c715c9b8f8173eefe90a135fb8a85ff91a1b3786
4
+ data.tar.gz: 5885d3ac9bfc90436e0c2f3a9afe00afc89faf7d
5
5
  SHA512:
6
- metadata.gz: 059fd1a5e57efc3cd8f9c2d7e7129508562210efcdf18e32b93b4921ba197e02cbd94a696903623191f4cd2d39a933a6c4d73a85fbfe96e3114744d8a5794d12
7
- data.tar.gz: 339a1e655dc420c4577bdce3e4567b5ccdea31e8e4a6b3fa882552dae38fcb35de2a5d5fbcbc3a452270c81414d39fdadffd0e02276be534e792116ebd84fc0e
6
+ metadata.gz: 1007ac7dff2535a558fd82715fa8871546d2c7f0aa9118020210e58193cba52b51e9abd294c73ac61ec54ae3c3ca4c499d656b650571d2035da25748e646d7ae
7
+ data.tar.gz: 65db7c3abb9f20f4b41882138b14a068c487d57bf61c923c2460994bfb2a346bf7541f8bb20f85d8868d38c39d6bd810126f4f0c9b7b28cd12c96b58cf7b4ca4
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
  |:----------- |:------------ |
27
27
  |[TbpgrUtils Array#>>](#array) |return ArrayContext for each execute |
28
28
  |[TbpgrUtils Array#average](#arrayaverage) |return average |
29
+ |[TbpgrUtils Array#exchange](#arrayexchange ) |exchange array's elements |
29
30
  |[TbpgrUtils Array#kernel_send](#arraykernel_send) |alias of map {|v|send :some_kernel_method, v} |
30
31
  |[TbpgrUtils Array#sum](#arraysum) |alias of Array#reduce(&:+). |
31
32
  |[TbpgrUtils Array#to_table](#arrayto_table) |Array(Array, Array...) to table format. |
@@ -186,6 +187,17 @@ require 'tbpgr_utils'
186
187
 
187
188
  [back to list](#list)
188
189
 
190
+ ### Array#exchange
191
+ ~~~ruby
192
+ require 'tbpgr_utils'
193
+ [*1..6].exchange!(1, 5) # => [1, 6, 3, 4, 5, 2]
194
+ [*1..6].exchange!(1, -1) # => [1, 6, 3, 4, 5, 2]
195
+ [*1..6].exchange!(1, 6) # => [*1..6]
196
+ [].exchange!(1, 2) # => []
197
+ ~~~
198
+
199
+ [back to list](#list)
200
+
189
201
  ### Array#kernel_send
190
202
  ~~~ruby
191
203
  require 'tbpgr_utils'
@@ -3637,6 +3649,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
3637
3649
  https://github.com/tbpgr/tbpgr_utils_snippets
3638
3650
 
3639
3651
  ## History
3652
+ * version 0.0.128 : add Array#exchange, change Kernel#bulk_puts_eval output format(justify)
3640
3653
  * version 0.0.127 : add String#uniq_size
3641
3654
  * version 0.0.126 : add Array#uniq_size
3642
3655
  * version 0.0.125 : add Hash#>>
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ # Array
4
+ class Array
5
+ # exchange arrays elements
6
+ #
7
+ # === Example
8
+ #
9
+ # [*1..6].exchange!(1, 5) # => [1, 6, 3, 4, 5, 2]
10
+ # [*1..6].exchange!(1, -1) # => [1, 6, 3, 4, 5, 2]
11
+ # [*1..6].exchange!(1, 6) # => [*1..6]
12
+ # [].exchange!(1, 2) # => []
13
+ #
14
+ def exchange!(one_index, other_index)
15
+ return self unless one_index.respond_to? :to_i
16
+ return self unless other_index.respond_to? :to_i
17
+ one_index = one_index.to_i
18
+ other_index = other_index.to_i
19
+ return self if one_index.abs >= size
20
+ return self if other_index.abs >= size
21
+ tmp_one = self[one_index]
22
+ self[one_index] = self[other_index]
23
+ self[other_index] = tmp_one
24
+ self
25
+ end
26
+ end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'open_classes/array/average'
3
+ require 'open_classes/array/exchange'
3
4
  require 'open_classes/array/gte_gte'
4
5
  require 'open_classes/array/kernel_send'
5
6
  require 'open_classes/array/sum'
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+ require 'open_classes/string'
2
3
 
3
4
  # Kernel
4
5
  module Kernel
@@ -31,7 +32,9 @@ module Kernel
31
32
  # "hoge-#{message}1" # => "hoge-msg1"\n
32
33
  # "hoge-#{message}2" # => "hoge-msg2"\n
33
34
  def bulk_puts_eval(binding, codes)
34
- codes.each_line { |code|puts_eval(code.chop, binding) }
35
+ result = []
36
+ codes.each_line { |code|result << exec_eval(code.chop, binding) }
37
+ puts result.join("\n").justify_char '#'
35
38
  end
36
39
 
37
40
  private
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.127'
5
+ VERSION = '0.0.128'
6
6
  end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Array do
6
+ context :exchange! do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'normal case',
11
+ input: [*1..6],
12
+ one_index: 1,
13
+ other_index: 5,
14
+ expected: [1, 6, 3, 4, 5, 2],
15
+ },
16
+ {
17
+ case_no: 2,
18
+ case_title: 'minus case',
19
+ input: [*1..6],
20
+ one_index: 1,
21
+ other_index: -1,
22
+ expected: [1, 6, 3, 4, 5, 2],
23
+ },
24
+ {
25
+ case_no: 3,
26
+ case_title: 'out of range case',
27
+ input: [*1..6],
28
+ one_index: 1,
29
+ other_index: 6,
30
+ expected: [*1..6],
31
+ },
32
+ {
33
+ case_no: 4,
34
+ case_title: 'out of range(minus) case',
35
+ input: [*1..6],
36
+ one_index: 1,
37
+ other_index: -6,
38
+ expected: [*1..6],
39
+ },
40
+ {
41
+ case_no: 5,
42
+ case_title: 'empty case',
43
+ input: [],
44
+ one_index: 1,
45
+ other_index: 6,
46
+ expected: [],
47
+ },
48
+ ]
49
+
50
+ cases.each do |c|
51
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
52
+ begin
53
+ case_before c
54
+
55
+ # -- given --
56
+ # nothing
57
+
58
+ # -- when/then --
59
+ actual = c[:input].exchange! c[:one_index], c[:other_index]
60
+
61
+ expect(actual).to eq(c[:expected])
62
+ ensure
63
+ case_after c
64
+ end
65
+ end
66
+
67
+ def case_before(c)
68
+ # implement each case before
69
+ end
70
+
71
+ def case_after(c)
72
+ # implement each case after
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,71 +1,71 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
- require 'open_classes/kernel'
4
- require 'test_toolbox'
5
-
6
- describe Kernel do
7
- context :bluk_puts_eval do
8
- BULK_CODE1 = <<-EOS
9
- "hoge-hige1" + "add"
10
- "hoge-hige2" + "add"
11
- EOS
12
-
13
- BULK_EXPECTED1 = <<-EOS
14
- "hoge-hige1" + "add" # => "hoge-hige1add"
15
- "hoge-hige2" + "add" # => "hoge-hige2add"
16
- EOS
17
-
18
- BULK_CODE2 = <<-EOS
19
- "hoge-hige1" + "add" + message
20
- "hoge-hige2" + "add" + message
21
- EOS
22
-
23
- BULK_EXPECTED2 = <<-EOS
24
- "hoge-hige1" + "add" + message # => "hoge-hige1addmsg"
25
- "hoge-hige2" + "add" + message # => "hoge-hige2addmsg"
26
- EOS
27
-
28
- cases = [
29
- {
30
- case_no: 1,
31
- case_title: 'no bind case',
32
- code: BULK_CODE1,
33
- expected: BULK_EXPECTED1
34
- },
35
- {
36
- case_no: 2,
37
- case_title: 'with bind case',
38
- code: BULK_CODE1,
39
- bind: 'msg',
40
- expected: BULK_EXPECTED1
41
- },
42
- ]
43
-
44
- cases.each do |c|
45
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
46
- begin
47
- case_before c
48
-
49
- # -- given --
50
- message = c[:bind] if c[:bind]
51
-
52
- # -- when --
53
- actual = capture_stdout { bulk_puts_eval binding, c[:code] }
54
-
55
- # -- then --
56
- expect(actual).to eq(c[:expected])
57
- ensure
58
- case_after c
59
- end
60
- end
61
-
62
- def case_before(c)
63
- # implement each case before
64
- end
65
-
66
- def case_after(c)
67
- # implement each case after
68
- end
69
- end
70
- end
71
- end
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'open_classes/kernel'
4
+ require 'test_toolbox'
5
+
6
+ describe Kernel do
7
+ context :bluk_puts_eval do
8
+ BULK_CODE1 = <<-EOS
9
+ "hoge-hige1" + "add"
10
+ "hoge-hige2" + "add"
11
+ EOS
12
+
13
+ BULK_EXPECTED1 = <<-EOS
14
+ "hoge-hige1" + "add" # => "hoge-hige1add"
15
+ "hoge-hige2" + "add" # => "hoge-hige2add"
16
+ EOS
17
+
18
+ BULK_CODE2 = <<-EOS
19
+ "hoge-hige1" + "add" + message
20
+ "hoge-hige2" + "add" + message
21
+ EOS
22
+
23
+ BULK_EXPECTED2 = <<-EOS
24
+ "hoge-hige1" + "add" + message # => "hoge-hige1addmsg"
25
+ "hoge-hige2" + "add" + message # => "hoge-hige2addmsg"
26
+ EOS
27
+
28
+ cases = [
29
+ {
30
+ case_no: 1,
31
+ case_title: 'no bind case',
32
+ code: BULK_CODE1,
33
+ expected: BULK_EXPECTED1
34
+ },
35
+ {
36
+ case_no: 2,
37
+ case_title: 'with bind case',
38
+ code: BULK_CODE1,
39
+ bind: 'msg',
40
+ expected: BULK_EXPECTED1
41
+ },
42
+ ]
43
+
44
+ cases.each do |c|
45
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
46
+ begin
47
+ case_before c
48
+
49
+ # -- given --
50
+ message = c[:bind] if c[:bind]
51
+
52
+ # -- when --
53
+ actual = capture_stdout { bulk_puts_eval binding, c[:code] }
54
+
55
+ # -- then --
56
+ expect(actual).to eq(c[:expected])
57
+ ensure
58
+ case_after c
59
+ end
60
+ end
61
+
62
+ def case_before(c)
63
+ # implement each case before
64
+ end
65
+
66
+ def case_after(c)
67
+ # implement each case after
68
+ end
69
+ end
70
+ end
71
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.127
4
+ version: 0.0.128
5
5
  platform: ruby
6
6
  authors:
7
7
  - tbpgr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-14 00:00:00.000000000 Z
11
+ date: 2014-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -136,6 +136,7 @@ files:
136
136
  - lib/metasyntactic_variable.rb
137
137
  - lib/open_classes/array.rb
138
138
  - lib/open_classes/array/average.rb
139
+ - lib/open_classes/array/exchange.rb
139
140
  - lib/open_classes/array/gte_gte.rb
140
141
  - lib/open_classes/array/kernel_send.rb
141
142
  - lib/open_classes/array/sum.rb
@@ -277,6 +278,7 @@ files:
277
278
  - spec/markdown/ul_spec.rb
278
279
  - spec/metasyntactic_variable_spec.rb
279
280
  - spec/open_classes/array/average_spec.rb
281
+ - spec/open_classes/array/exchange_spec.rb
280
282
  - spec/open_classes/array/gte_gte_spec.rb
281
283
  - spec/open_classes/array/kernel_send_spec.rb
282
284
  - spec/open_classes/array/sum_spec.rb
@@ -434,6 +436,7 @@ test_files:
434
436
  - spec/markdown/ul_spec.rb
435
437
  - spec/metasyntactic_variable_spec.rb
436
438
  - spec/open_classes/array/average_spec.rb
439
+ - spec/open_classes/array/exchange_spec.rb
437
440
  - spec/open_classes/array/gte_gte_spec.rb
438
441
  - spec/open_classes/array/kernel_send_spec.rb
439
442
  - spec/open_classes/array/sum_spec.rb