--0016363b9d92cc4f83047afded6a
Content-Type: text/plain; charset=ISO-8859-1
Hi,
It doesn't look too usefull to me like that. (I mean using classes to
compare).
For this purpose I wrote something like:
# Analyse arguments given to a method(*args)
#
# Exemple:
# def m(*args)
# case args
# when ARGS[Complex]
# m(Complex.new(1,1))
# when ARGS[Integer, Boolean]
# m(2, true)
# when ARGS[[Numeric, Float], String]
# m([1, 3.14], "Hello World!")
# when ARGS[[[Integer, Integer],[Float, Rational ]]]
# m( [[1 , 2 ],[3.0 , Rational(1,2)]])
# end
# end
module Boolean; end
class FalseClass; include Boolean; end
class TrueClass; include Boolean; end
class ARGS
def initialize(*constraints)
@constraints onstraints
end
def ARGS.[](*constraints)
ARGS.new(*constraints)
end
def match?(args)
return false unless @constraints.length args.length
@constraints.each_with_index { |constraint, i|
case constraint
when Module
unless args[i].is_a?(constraint)
return false
end
when Array
unless args[i].is_a?(Array) &&
ARGS[*constraint].match?(args[i])
return false
end
end
}
true
end
def (args)
match?(args)
end
end
-------
Some mails ago, I also thought Array# implemented a OR-related test like
in:
case 2
when -1,1,2
...
end
But that seems to be more a feature of the "block" case.
So, I think this trick can be useful in special "cases", but I don't see
enough interest to do that for the Ruby core.
Any exemples more attractive ?
2009/12/18 Dmitry Vazhov <dmitryelastic / gmail.com>
> So, my question is: Do you agree that Array# can be useful for us?
>
> My opinion is that it can be in ruby std library along with
> Proc# (which is already in ruby 1.9 --
>
> http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/
> )
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
--0016363b9d92cc4f83047afded6a--