D
Dmitry Vazhov
Hello,
We already have :=== operator defined in Module, Range, Regexp and Proc
which is great for case/when statement. For all other objects :=== mean
:==.
My suggestion is to extend Array with === method:
Original behaviour:
[1,2,3] === [1,2,3] #=> true
[1,2,3] === [1,2,4] #=> false
[1,2,3] === 2 #=> false
Adding code: -- this code is similar to Array#== , but uses :=== for
comparing each element
class Array
def ===( arg )
arg.is_a?(Array) && self.size == arg.size && (0...size).all?{|i|
self === arg }
end
end
After adding code:
[1,2,3] === [1,2,3] #=> true
[1,2,3] === 2 #=> false
[1,2,Object] === [1,2,3] #=> true
[1,2,/^some/] === [1,2,"some string"] #=> true
[1,2,[Symbol, Array]] === [1,2,[:key, []]] #=> true <---- this
is an example of deep structure matching
You can see other examples at
http://github.com/dmitryelastic/dumb-patterns-matching/blob/master/patterns_matching.rb
and http://github.com/dmitryelastic/dumb-patterns-matching
We already have :=== operator defined in Module, Range, Regexp and Proc
which is great for case/when statement. For all other objects :=== mean
:==.
My suggestion is to extend Array with === method:
Original behaviour:
[1,2,3] === [1,2,3] #=> true
[1,2,3] === [1,2,4] #=> false
[1,2,3] === 2 #=> false
Adding code: -- this code is similar to Array#== , but uses :=== for
comparing each element
class Array
def ===( arg )
arg.is_a?(Array) && self.size == arg.size && (0...size).all?{|i|
self === arg }
end
end
After adding code:
[1,2,3] === [1,2,3] #=> true
[1,2,3] === 2 #=> false
[1,2,Object] === [1,2,3] #=> true
[1,2,/^some/] === [1,2,"some string"] #=> true
[1,2,[Symbol, Array]] === [1,2,[:key, []]] #=> true <---- this
is an example of deep structure matching
You can see other examples at
http://github.com/dmitryelastic/dumb-patterns-matching/blob/master/patterns_matching.rb
and http://github.com/dmitryelastic/dumb-patterns-matching