tbpgr_utils 0.0.150 → 0.0.151
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +9 -9
- data/README.md +95 -32
- data/lib/attr_enumerable/attr_enumerable_helper.rb +52 -52
- data/lib/attr_enumerable/map_attr.rb +1 -1
- data/lib/eval_helper/attr_accessor_init_code.rb +3 -3
- data/lib/eval_helper/each_do_code.rb +26 -26
- data/lib/eval_helper/each_with_index_do_code.rb +26 -26
- data/lib/eval_helper/require_code.rb +1 -1
- data/lib/eval_helper/require_relative_code.rb +1 -1
- data/lib/eval_helper/set_variable_code.rb +1 -1
- data/lib/eval_helper/set_variables_code.rb +1 -1
- data/lib/eval_helper/times_code.rb +1 -1
- data/lib/ghostable.rb +1 -1
- data/lib/open_classes/array.rb +0 -2
- data/lib/open_classes/array/average.rb +17 -17
- data/lib/open_classes/array/gte_gte.rb +1 -1
- data/lib/open_classes/array/to_table.rb +1 -1
- data/lib/open_classes/array/together_clear.rb +24 -24
- data/lib/open_classes/array/together_compact.rb +44 -44
- data/lib/open_classes/array/together_empty.rb +1 -1
- data/lib/open_classes/array/together_include.rb +1 -1
- data/lib/open_classes/enumerable.rb +4 -0
- data/lib/open_classes/enumerable/if_else_map.rb +12 -0
- data/lib/open_classes/{array → enumerable}/kernel_send.rb +15 -15
- data/lib/open_classes/{array → enumerable}/sum.rb +3 -3
- data/lib/open_classes/hash/gte_gte.rb +45 -45
- data/lib/open_classes/integer/each_digit.rb +13 -13
- data/lib/open_classes/integer/each_digit_with_index.rb +13 -13
- data/lib/open_classes/integer/reverse_each_digit.rb +13 -13
- data/lib/open_classes/kernel/aa_ancestors.rb +1 -1
- data/lib/open_classes/numeric/is_ascii.rb +4 -4
- data/lib/open_classes/object/any_of.rb +1 -2
- data/lib/open_classes/object/to_bool.rb +1 -1
- data/lib/open_classes/string/ascii_unicode_html_table.rb +1 -1
- data/lib/open_classes/string/ascii_unicode_table.rb +1 -1
- data/lib/open_classes/string/is_meta_variable.rb +7 -7
- data/lib/open_classes/string/justify_table.rb +2 -2
- data/lib/open_classes/string/surround.rb +2 -2
- data/lib/open_classes/symbol/is_meta_variable.rb +6 -6
- data/lib/tbpgr_utils.rb +1 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/lib/templatable.rb +1 -1
- data/rubocop-todo.yml +2 -36
- data/spec/attr_enumerable/each_attr_spec.rb +1 -1
- data/spec/attr_enumerable/each_attr_with_index_spec.rb +1 -1
- data/spec/attr_enumerable/first_attr_spec.rb +107 -107
- data/spec/attr_enumerable/include_attr_spec.rb +4 -4
- data/spec/attr_enumerable/last_attr_spec.rb +4 -4
- data/spec/attr_enumerable/map_attr_spec.rb +3 -3
- data/spec/attr_enumerable/reduce_attr_spec.rb +3 -3
- data/spec/attr_enumerable/reverse_attr_spec.rb +1 -1
- data/spec/attr_enumerable/sample_attr_spec.rb +3 -3
- data/spec/attr_enumerable/select_attr_spec.rb +2 -2
- data/spec/attr_enumerable/shuffle_attr_spec.rb +1 -1
- data/spec/attr_enumerable/slice_attr_spec.rb +1 -1
- data/spec/attributes_hashable_spec.rb +1 -1
- data/spec/attributes_initializable_spec.rb +21 -21
- data/spec/eval_helper/attr_accessor_init_code_spec.rb +1 -1
- data/spec/eval_helper/attr_init_class_code_spec.rb +1 -1
- data/spec/eval_helper/each_brace_code_spec.rb +2 -2
- data/spec/eval_helper/each_do_code_spec.rb +1 -1
- data/spec/eval_helper/each_with_index_brace_code_spec.rb +2 -2
- data/spec/eval_helper/each_with_index_do_code_spec.rb +2 -2
- data/spec/eval_helper/if_code_after_spec.rb +3 -3
- data/spec/eval_helper/if_code_spec.rb +3 -3
- data/spec/eval_helper/require_code_spec.rb +3 -3
- data/spec/eval_helper/require_relative_code_spec.rb +3 -3
- data/spec/eval_helper/set_variable_code_spec.rb +3 -3
- data/spec/{set_variables_code_spec.rb → eval_helper/set_variables_code_spec.rb} +15 -15
- data/spec/eval_helper/ternary_operator_spec.rb +5 -5
- data/spec/eval_helper/times_code_spec.rb +1 -1
- data/spec/eval_helper/unless_code_after_spec.rb +3 -3
- data/spec/eval_helper/unless_code_spec.rb +3 -3
- data/spec/familyable/familyable_spec.rb +6 -6
- data/spec/ghostable_spec.rb +5 -5
- data/spec/markdown/backquotes_spec.rb +4 -4
- data/spec/markdown/bold_spec.rb +4 -4
- data/spec/markdown/code_spec.rb +3 -3
- data/spec/markdown/codes_spec.rb +2 -2
- data/spec/markdown/heading_spec.rb +5 -5
- data/spec/markdown/hr_spec.rb +1 -1
- data/spec/markdown/italic_spec.rb +4 -4
- data/spec/markdown/link_spec.rb +1 -1
- data/spec/markdown/ol_spec.rb +3 -3
- data/spec/markdown/ul_spec.rb +3 -3
- data/spec/metasyntactic_variable_spec.rb +4 -4
- data/spec/open_classes/array/average_spec.rb +4 -4
- data/spec/open_classes/array/exchange_spec.rb +6 -6
- data/spec/open_classes/array/gte_gte_spec.rb +4 -4
- data/spec/open_classes/array/to_html_table_spec.rb +3 -3
- data/spec/open_classes/array/to_table_spec.rb +2 -2
- data/spec/open_classes/array/together_at_spec.rb +4 -4
- data/spec/open_classes/array/together_clear_spec.rb +3 -3
- data/spec/open_classes/array/together_compact_spec.rb +6 -6
- data/spec/open_classes/array/together_concat_spec.rb +3 -3
- data/spec/open_classes/array/together_delete_at_spec.rb +5 -5
- data/spec/open_classes/array/together_delete_if_spec.rb +4 -4
- data/spec/open_classes/array/together_delete_spec.rb +5 -5
- data/spec/open_classes/array/together_empty_spec.rb +4 -4
- data/spec/open_classes/array/together_fill_spec.rb +5 -5
- data/spec/open_classes/array/together_first_spec.rb +6 -6
- data/spec/open_classes/array/together_include_spec.rb +8 -8
- data/spec/open_classes/array/together_index_spec.rb +5 -5
- data/spec/open_classes/array/together_insert_spec.rb +5 -5
- data/spec/open_classes/array/together_last_spec.rb +6 -6
- data/spec/open_classes/array/together_map_spec.rb +14 -14
- data/spec/open_classes/array/together_pop_spec.rb +8 -8
- data/spec/open_classes/array/together_reduce_spec.rb +8 -8
- data/spec/open_classes/array/together_reverse_spec.rb +119 -119
- data/spec/open_classes/array/together_sample_spec.rb +8 -8
- data/spec/open_classes/array/together_select_spec.rb +6 -6
- data/spec/open_classes/array/together_shift_spec.rb +8 -8
- data/spec/open_classes/array/together_shuffle_spec.rb +3 -3
- data/spec/open_classes/array/together_slice_spec.rb +5 -5
- data/spec/open_classes/array/together_spec.rb +3 -3
- data/spec/open_classes/array/together_with_index_spec.rb +3 -3
- data/spec/open_classes/array/uniq_size_spec.rb +4 -4
- data/spec/open_classes/enumerable/if_else_map_spec.rb +45 -0
- data/spec/open_classes/{array → enumerable}/kernel_send_spec.rb +50 -43
- data/spec/open_classes/{array → enumerable}/sum_spec.rb +8 -2
- data/spec/open_classes/fixnum/to_fixnum_html_table_spec.rb +1 -1
- data/spec/open_classes/fixnum/to_fixnum_table_spec.rb +1 -1
- data/spec/open_classes/hash/gte_gte_spec.rb +4 -4
- data/spec/open_classes/hash/html_table_spec.rb +2 -2
- data/spec/open_classes/hash/table_spec.rb +2 -2
- data/spec/open_classes/integer/each_digit_spec.rb +2 -2
- data/spec/open_classes/integer/each_digit_with_index_spec.rb +2 -2
- data/spec/open_classes/integer/palindromic_prime_spec.rb +6 -6
- data/spec/open_classes/integer/reverse_each_digit_spec.rb +2 -2
- data/spec/open_classes/kernel/aa_ancestors_spec.rb +1 -1
- data/spec/open_classes/kernel/booleans_spec.rb +1 -1
- data/spec/open_classes/kernel/bulk_define_methods_spec.rb +5 -5
- data/spec/open_classes/kernel/bulk_puts_eval_spec.rb +1 -1
- data/spec/open_classes/kernel/evalb_spec.rb +2 -2
- data/spec/open_classes/kernel/exchange_spec.rb +2 -2
- data/spec/open_classes/kernel/hash_to_attributes_spec.rb +3 -3
- data/spec/open_classes/kernel/null_spec.rb +1 -1
- data/spec/open_classes/kernel/print_eval_spec.rb +3 -3
- data/spec/open_classes/kernel/puts_eval_spec.rb +3 -3
- data/spec/open_classes/module_spec.rb +3 -3
- data/spec/open_classes/numeric/dice_back_spec.rb +8 -8
- data/spec/open_classes/numeric/dozen_spec.rb +4 -4
- data/spec/open_classes/numeric/is_ascii_spec.rb +6 -6
- data/spec/open_classes/numeric/to_binary_html_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_binary_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_digit_html_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_digit_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_hex_html_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_hex_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_oct_html_table_spec.rb +1 -1
- data/spec/open_classes/numeric/to_oct_table_spec.rb +1 -1
- data/spec/open_classes/object/any_of_spec.rb +6 -6
- data/spec/open_classes/object/boolean_spec.rb +6 -6
- data/spec/open_classes/object/grep_instance_method_spec.rb +15 -15
- data/spec/open_classes/object/grep_method_spec.rb +6 -6
- data/spec/open_classes/object/guard_spec.rb +8 -8
- data/spec/open_classes/object/method_nameable_spec.rb +4 -4
- data/spec/open_classes/object/my_methods_spec.rb +2 -2
- data/spec/open_classes/object/null_spec.rb +1 -1
- data/spec/open_classes/object/to_bool_spec.rb +7 -7
- data/spec/open_classes/string/ascii1_other2_size_spec.rb +5 -5
- data/spec/open_classes/string/ascii_unicode_html_table_spec.rb +1 -1
- data/spec/open_classes/string/ascii_unicode_table_spec.rb +1 -1
- data/spec/open_classes/string/comma_to_a_spec.rb +1 -1
- data/spec/open_classes/string/cygwinpath_to_winpath_spec.rb +3 -3
- data/spec/open_classes/string/escape_double_quote_spec.rb +3 -3
- data/spec/open_classes/string/escape_quote_spec.rb +3 -3
- data/spec/open_classes/string/gte_gte_spec.rb +3 -3
- data/spec/open_classes/string/hyphen_to_a_spec.rb +1 -1
- data/spec/open_classes/string/is_meta_variable_spec.rb +3 -3
- data/spec/open_classes/string/justify_char_spec.rb +6 -6
- data/spec/open_classes/string/justify_table_spec.rb +6 -6
- data/spec/open_classes/string/say_spec.rb +2 -2
- data/spec/open_classes/string/spacing_spec.rb +6 -6
- data/spec/open_classes/string/surround_spec.rb +1 -1
- data/spec/open_classes/string/table_to_array_spec.rb +1 -1
- data/spec/open_classes/string/to_hatena_heading_spec.rb +5 -5
- data/spec/open_classes/string/to_markdown_heading_spec.rb +5 -5
- data/spec/open_classes/string/to_space2_heading_spec.rb +5 -5
- data/spec/open_classes/string/to_space4_heading_spec.rb +5 -5
- data/spec/open_classes/string/to_tab_heading_spec.rb +5 -5
- data/spec/open_classes/string/unescape_double_quote_spec.rb +3 -3
- data/spec/open_classes/string/unescape_quote_spec.rb +3 -3
- data/spec/open_classes/string/uniq_size_spec.rb +4 -4
- data/spec/open_classes/string/uniq_spec.rb +4 -4
- data/spec/open_classes/string/winpath_to_cygwinpath_spec.rb +3 -3
- data/spec/open_classes/symbol/is_meta_variable_spec.rb +3 -3
- data/spec/simple_tournament_spec.rb +9 -9
- data/spec/templatable_spec.rb +2 -2
- data/spec/template_methodable_spec.rb +1 -1
- data/spec/test_toolbox/kernel_spec.rb +7 -7
- data/tbpgr_utils.gemspec +4 -4
- metadata +15 -11
@@ -32,7 +32,7 @@ module EvalHelper
|
|
32
32
|
# name1 = "value1"
|
33
33
|
# name2 = "value2"
|
34
34
|
#
|
35
|
-
def
|
35
|
+
def setting_variables_code(variables)
|
36
36
|
fail TypeError, "invalid type #{variables.class}. you have to use Array" unless variables.is_a? Array
|
37
37
|
variables.reduce([]) do |ret, variable|
|
38
38
|
fail TypeError, "invalid type #{variable.class}. you have to use Hash" unless variable.is_a? Hash
|
@@ -50,7 +50,7 @@ module EvalHelper
|
|
50
50
|
#
|
51
51
|
def times_code(number, proc)
|
52
52
|
return "#{number}.times { |i| #{proc} }" if proc.count('\n') == 0
|
53
|
-
indented = proc.split('\n').reduce([]) { |
|
53
|
+
indented = proc.split('\n').reduce([]) { |a, e|a << " #{e}"; a }.join("\n")
|
54
54
|
<<-EOS
|
55
55
|
#{number}.times do |i|
|
56
56
|
#{indented}
|
data/lib/ghostable.rb
CHANGED
@@ -21,7 +21,7 @@ module Ghostable
|
|
21
21
|
|
22
22
|
def method_missing(method_name, *args, &block)
|
23
23
|
get_patterns.each do |basename, pattern|
|
24
|
-
return method(basename).call(method_name, *args, &block) if method_name.to_s.match
|
24
|
+
return method(basename).call(method_name, *args, &block) if method_name.to_s.match(/#{pattern}/)
|
25
25
|
end
|
26
26
|
super
|
27
27
|
end
|
data/lib/open_classes/array.rb
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
require 'open_classes/array/average'
|
3
3
|
require 'open_classes/array/exchange'
|
4
4
|
require 'open_classes/array/gte_gte'
|
5
|
-
require 'open_classes/array/kernel_send'
|
6
|
-
require 'open_classes/array/sum'
|
7
5
|
require 'open_classes/array/to_html_table'
|
8
6
|
require 'open_classes/array/to_table'
|
9
7
|
require 'open_classes/array/together'
|
@@ -1,17 +1,17 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Array
|
4
|
-
class Array
|
5
|
-
# return average
|
6
|
-
#
|
7
|
-
# === Example
|
8
|
-
#
|
9
|
-
# [*1..6].average # => 3.5
|
10
|
-
# [1.5, 2.5].average # => 2.0
|
11
|
-
# [*'a'..'z'].average # => raise TypeError
|
12
|
-
#
|
13
|
-
def average
|
14
|
-
fail TypeError, 'you have to use elements that is Numeric'
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Array
|
4
|
+
class Array
|
5
|
+
# return average
|
6
|
+
#
|
7
|
+
# === Example
|
8
|
+
#
|
9
|
+
# [*1..6].average # => 3.5
|
10
|
+
# [1.5, 2.5].average # => 2.0
|
11
|
+
# [*'a'..'z'].average # => raise TypeError
|
12
|
+
#
|
13
|
+
def average
|
14
|
+
fail TypeError, 'you have to use elements that is Numeric' unless all? { |v|v.is_a? Numeric }
|
15
|
+
sum.to_f / size
|
16
|
+
end
|
17
|
+
end
|
@@ -15,7 +15,7 @@ class Array
|
|
15
15
|
#
|
16
16
|
def to_table(position = :right)
|
17
17
|
ret = reduce([]) do |rets, lines|
|
18
|
-
ret = lines.reduce([]) { |
|
18
|
+
ret = lines.reduce([]) { |a, e|a << e; a }
|
19
19
|
rets << "|#{ret.join('|')}|"
|
20
20
|
end.join("\n") + "\n"
|
21
21
|
ret.justify_table(position)
|
@@ -1,24 +1,24 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'open_classes/object'
|
3
|
-
require 'open_classes/module'
|
4
|
-
require 'open_classes/array/together_helper'
|
5
|
-
|
6
|
-
# Array
|
7
|
-
class Array
|
8
|
-
include TogetherHelper
|
9
|
-
|
10
|
-
# Arrays bulk clear.
|
11
|
-
#
|
12
|
-
# together_clear has alias :tclear
|
13
|
-
#
|
14
|
-
# same elements size case
|
15
|
-
# alpha = %w{one two three}
|
16
|
-
# numbers = %w{1 2 3}
|
17
|
-
# [alpha, numbers].together_clear # => output [[],[]]
|
18
|
-
def together_clear
|
19
|
-
if_not_contain_array_rails_type_error
|
20
|
-
each
|
21
|
-
end
|
22
|
-
|
23
|
-
alias_method :tclear, :together_clear
|
24
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk clear.
|
11
|
+
#
|
12
|
+
# together_clear has alias :tclear
|
13
|
+
#
|
14
|
+
# same elements size case
|
15
|
+
# alpha = %w{one two three}
|
16
|
+
# numbers = %w{1 2 3}
|
17
|
+
# [alpha, numbers].together_clear # => output [[],[]]
|
18
|
+
def together_clear
|
19
|
+
if_not_contain_array_rails_type_error
|
20
|
+
each(&:clear)
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :tclear, :together_clear
|
24
|
+
end
|
@@ -1,44 +1,44 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'open_classes/object'
|
3
|
-
require 'open_classes/module'
|
4
|
-
require 'open_classes/array/together_helper'
|
5
|
-
|
6
|
-
# Array
|
7
|
-
class Array
|
8
|
-
include TogetherHelper
|
9
|
-
|
10
|
-
# Arrays bulk compact.(immutable)
|
11
|
-
#
|
12
|
-
# together_compact has alias :tcompact
|
13
|
-
#
|
14
|
-
# same elements size case
|
15
|
-
# alpha = ['a','b','c', nil,'d']
|
16
|
-
# numbers = [1, 2, nil, 3]
|
17
|
-
# lists = [alpha, numbers]
|
18
|
-
# ret = lists.together_compact
|
19
|
-
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
20
|
-
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
21
|
-
def together_compact
|
22
|
-
if_not_contain_array_rails_type_error
|
23
|
-
reduce([]) { |ret, list|ret << list.compact }
|
24
|
-
end
|
25
|
-
|
26
|
-
# Arrays bulk compact!.(mutable)
|
27
|
-
#
|
28
|
-
# together_compact! has alias :tcompact!
|
29
|
-
#
|
30
|
-
# same elements size case
|
31
|
-
# alpha = ['a','b','c', nil,'d']
|
32
|
-
# numbers = [1, 2, nil, 3]
|
33
|
-
# lists = [alpha, numbers]
|
34
|
-
# ret = lists.together_compact!
|
35
|
-
# print lists # => output [['a','b','c','d'], [1, 2, 3]]
|
36
|
-
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
37
|
-
def together_compact!
|
38
|
-
if_not_contain_array_rails_type_error
|
39
|
-
each
|
40
|
-
end
|
41
|
-
|
42
|
-
alias_method :tcompact, :together_compact
|
43
|
-
alias_method :tcompact!, :together_compact!
|
44
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk compact.(immutable)
|
11
|
+
#
|
12
|
+
# together_compact has alias :tcompact
|
13
|
+
#
|
14
|
+
# same elements size case
|
15
|
+
# alpha = ['a','b','c', nil,'d']
|
16
|
+
# numbers = [1, 2, nil, 3]
|
17
|
+
# lists = [alpha, numbers]
|
18
|
+
# ret = lists.together_compact
|
19
|
+
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
20
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
21
|
+
def together_compact
|
22
|
+
if_not_contain_array_rails_type_error
|
23
|
+
reduce([]) { |ret, list|ret << list.compact }
|
24
|
+
end
|
25
|
+
|
26
|
+
# Arrays bulk compact!.(mutable)
|
27
|
+
#
|
28
|
+
# together_compact! has alias :tcompact!
|
29
|
+
#
|
30
|
+
# same elements size case
|
31
|
+
# alpha = ['a','b','c', nil,'d']
|
32
|
+
# numbers = [1, 2, nil, 3]
|
33
|
+
# lists = [alpha, numbers]
|
34
|
+
# ret = lists.together_compact!
|
35
|
+
# print lists # => output [['a','b','c','d'], [1, 2, 3]]
|
36
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
37
|
+
def together_compact!
|
38
|
+
if_not_contain_array_rails_type_error
|
39
|
+
each(&:compact!)
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :tcompact, :together_compact
|
43
|
+
alias_method :tcompact!, :together_compact!
|
44
|
+
end
|
@@ -43,7 +43,7 @@ class Array
|
|
43
43
|
def together_include?(value, is_multi = false)
|
44
44
|
if_not_contain_array_rails_type_error
|
45
45
|
return reduce([]) { |ret, list|ret << list.include?(value) } if is_multi
|
46
|
-
reduce(false) { |ret, list|ret
|
46
|
+
reduce(false) { |ret, list|ret ||= list.include?(value) }
|
47
47
|
end
|
48
48
|
|
49
49
|
alias_method :tinclude?, :together_include?
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Enumerable
|
4
|
+
module Enumerable
|
5
|
+
# === Example
|
6
|
+
#
|
7
|
+
# [*1..4].if_else_map(:odd?.to_proc,->(odd){'奇数'},->(even){'偶数'}) # => ['奇数','偶数','奇数','偶数']
|
8
|
+
#
|
9
|
+
def if_else_map(predicate, proc_t, proc_f)
|
10
|
+
map { |e|predicate.call(e) ? proc_t.call(e) : proc_f.call(e) }
|
11
|
+
end
|
12
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
#
|
4
|
-
|
5
|
-
# alias of map {|v|send :some_kernel_method, v}
|
6
|
-
#
|
7
|
-
# === Example
|
8
|
-
#
|
9
|
-
# [*1..3].kernel_send:Rational # => [(1/1), (2/1), (3/1)]
|
10
|
-
#
|
11
|
-
def kernel_send(method_name)
|
12
|
-
return self unless [Symbol, String].include? method_name.class
|
13
|
-
map { |v|send method_name.to_sym, v }
|
14
|
-
end
|
15
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Enumerable
|
4
|
+
module Enumerable
|
5
|
+
# alias of map {|v|send :some_kernel_method, v}
|
6
|
+
#
|
7
|
+
# === Example
|
8
|
+
#
|
9
|
+
# [*1..3].kernel_send:Rational # => [(1/1), (2/1), (3/1)]
|
10
|
+
#
|
11
|
+
def kernel_send(method_name)
|
12
|
+
return self unless [Symbol, String].include? method_name.class
|
13
|
+
map { |v|send method_name.to_sym, v }
|
14
|
+
end
|
15
|
+
end
|
@@ -1,45 +1,45 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# HashContext
|
4
|
-
class HashContext
|
5
|
-
attr_reader :receiver
|
6
|
-
def initialize(receiver)
|
7
|
-
@receiver = receiver
|
8
|
-
end
|
9
|
-
|
10
|
-
def method_missing(method_name, *args
|
11
|
-
result = nil
|
12
|
-
if args.size > 0
|
13
|
-
receiver.reduce({}) do |ret, (key, value)|
|
14
|
-
value = value.send method_name, *args
|
15
|
-
ret[key] = value
|
16
|
-
ret
|
17
|
-
end
|
18
|
-
else
|
19
|
-
receiver.reduce({}) do |ret, (key
|
20
|
-
value = value.send method_name
|
21
|
-
ret[key] = value
|
22
|
-
ret
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def to_h
|
28
|
-
@receiver
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# Hash
|
33
|
-
class Hash
|
34
|
-
# return HashContext for each execute
|
35
|
-
#
|
36
|
-
# === Example
|
37
|
-
#
|
38
|
-
# h = {key1: "value1", key2: "value2"}
|
39
|
-
# h.>>.upcase # => {key1: "VALUE1", key2: "VALUE2"}
|
40
|
-
# h.>>.+('_hoge') # => {key1: "value1_hoge", key2: "value2_hoge"}
|
41
|
-
#
|
42
|
-
def >>(dummy = nil)
|
43
|
-
HashContext.new(self)
|
44
|
-
end
|
45
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# HashContext
|
4
|
+
class HashContext
|
5
|
+
attr_reader :receiver
|
6
|
+
def initialize(receiver)
|
7
|
+
@receiver = receiver
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(method_name, *args, &block)
|
11
|
+
result = nil
|
12
|
+
if args.size > 0
|
13
|
+
receiver.reduce({}) do |ret, (key, value)|
|
14
|
+
value = value.send method_name, *args
|
15
|
+
ret[key] = value
|
16
|
+
ret
|
17
|
+
end
|
18
|
+
else
|
19
|
+
receiver.reduce({}) do |ret, (key, value)|
|
20
|
+
value = value.send method_name
|
21
|
+
ret[key] = value
|
22
|
+
ret
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_h
|
28
|
+
@receiver
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Hash
|
33
|
+
class Hash
|
34
|
+
# return HashContext for each execute
|
35
|
+
#
|
36
|
+
# === Example
|
37
|
+
#
|
38
|
+
# h = {key1: "value1", key2: "value2"}
|
39
|
+
# h.>>.upcase # => {key1: "VALUE1", key2: "VALUE2"}
|
40
|
+
# h.>>.+('_hoge') # => {key1: "value1_hoge", key2: "value2_hoge"}
|
41
|
+
#
|
42
|
+
def >>(dummy = nil)
|
43
|
+
HashContext.new(self)
|
44
|
+
end
|
45
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
class Integer
|
4
|
-
# provide iterator for number's each digit
|
5
|
-
#
|
6
|
-
# === Example
|
7
|
-
#
|
8
|
-
# ret=[];12345.each_digit { |v|ret << v+1 };print ret # => [2,3,4,5,6]
|
9
|
-
#
|
10
|
-
def each_digit
|
11
|
-
abs.to_s.split('').map
|
12
|
-
end
|
13
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class Integer
|
4
|
+
# provide iterator for number's each digit
|
5
|
+
#
|
6
|
+
# === Example
|
7
|
+
#
|
8
|
+
# ret=[];12345.each_digit { |v|ret << v+1 };print ret # => [2,3,4,5,6]
|
9
|
+
#
|
10
|
+
def each_digit
|
11
|
+
abs.to_s.split('').map(&:to_i).each { |v|yield(v) }
|
12
|
+
end
|
13
|
+
end
|