From b746ac788e6e9ff8a143bff8c0d5adc4540911b2 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 15 Apr 2015 04:34:23 +0300 Subject: [PATCH 1/2] add Array#to_proc --- lib/core/facets/array/to_proc.rb | 36 ++++++++++++++++++++++++++++++++ test/core/array/test_to_proc.rb | 17 +++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/core/facets/array/to_proc.rb create mode 100644 test/core/array/test_to_proc.rb diff --git a/lib/core/facets/array/to_proc.rb b/lib/core/facets/array/to_proc.rb new file mode 100644 index 00000000..c750e3f0 --- /dev/null +++ b/lib/core/facets/array/to_proc.rb @@ -0,0 +1,36 @@ +class Array + + # Converts an associative array of method names and arguments + # to a Proc making chained calls on a given object. + # + # Examples + # + # [:to_i, :next, [:*, 2]].to_proc.call("2") + # # => 6 + # + # # Create a proc which calls "obj[1].is_a?(String)": + # chain = [[:[], 1], [:is_a?, String]] + # chain.to_proc + # + # the_hash = { one: "One", two: "Two", three: 3, four: nil } + # the_hash.select(&chain) + # # => { :one => "One", :two => "Two" } + # + # mapping = { "one" => "1", "two" => "2", "" => "0" } + # the_hash.values.map(&[:to_s, :downcase, [:sub, /one|two|$^/, mapping]]) + # # => ["1", "2", "3", "0"] + # + # Returns [Proc] + # + # :call-seq: + # [].to_proc + # &[:method1, [:method2, *args], [:method3, ...]] + # + + def to_proc + proc do |*obj| + obj = obj.shift if obj.size == 1 + reduce(obj) { |chain, (mth, *args)| chain.public_send(mth, *args) } + end + end unless method_defined?(:to_proc) +end \ No newline at end of file diff --git a/test/core/array/test_to_proc.rb b/test/core/array/test_to_proc.rb new file mode 100644 index 00000000..34380bfa --- /dev/null +++ b/test/core/array/test_to_proc.rb @@ -0,0 +1,17 @@ +covers 'facets/array/only.rb' + +test_case Array do + + method :only do + + test do + [5].only.assert == 5 + [nil].only.assert == nil + expect(IndexError){ [].only } + expect(IndexError){ [1,2,3].only } + end + + end + +end + From 11a0eff422edf62cb514030bf422d5abf87559a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B5nis=20Simo?= Date: Thu, 13 Oct 2016 19:12:46 +0300 Subject: [PATCH 2/2] Fix wrong tests --- test/core/array/test_to_proc.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/core/array/test_to_proc.rb b/test/core/array/test_to_proc.rb index 34380bfa..2c6b89ee 100644 --- a/test/core/array/test_to_proc.rb +++ b/test/core/array/test_to_proc.rb @@ -1,14 +1,14 @@ -covers 'facets/array/only.rb' +covers 'facets/array/to_proc.rb' test_case Array do - method :only do - + method :to_proc do test do - [5].only.assert == 5 - [nil].only.assert == nil - expect(IndexError){ [].only } - expect(IndexError){ [1,2,3].only } + the_hash = { one: "One", two: "Two", three: 3, four: nil } + the_hash.select(&[[:[], 1], [:is_a?, String]]). + assert == { :one => "One", :two => "Two" } + the_hash.values.map(&[:to_s, :downcase, [:sub, /one|two|$^/, { "one" => "1", "two" => "2", "" => "0" }]]). + assert == ["1", "2", "3", "0"] end end