From: Albert Schlef on
Robert Klemme wrote:
> A solution like this was proposed for overloading once but it does not
> seem to be widely used:
>
> irb(main):001:0> def overload(*a)
> irb(main):002:1> case a.map {|x|x.class}
> irb(main):003:2> when [String]
> irb(main):004:2> puts "a single string"
> irb(main):005:2> when [Fixnum, String]

Cool. Where is "Array#===" documented? I couldn't find it.
--
Posted via http://www.ruby-forum.com/.

From: Gary Wright on

On Jan 27, 2010, at 4:14 PM, Albert Schlef wrote:

> Robert Klemme wrote:
>> A solution like this was proposed for overloading once but it does not
>> seem to be widely used:
>>
>> irb(main):001:0> def overload(*a)
>> irb(main):002:1> case a.map {|x|x.class}
>> irb(main):003:2> when [String]
>> irb(main):004:2> puts "a single string"
>> irb(main):005:2> when [Fixnum, String]
>
> Cool. Where is "Array#===" documented? I couldn't find it.

Unfortunately Array#=== is just the same as Array#==, which is
why the argument list is mapped to class objects via #map in that
example from Robert.

There was a somewhat recent thread about changing Array#=== to
be an element-wise application of ===, which makes a lot of
sense to me.

Gary Wright

From: Albert Schlef on
Gary Wright wrote:
> On Jan 27, 2010, at 4:14 PM, Albert Schlef wrote:
>
>> Cool. Where is "Array#===" documented? I couldn't find it.
> Unfortunately Array#=== is just the same as Array#==,

So it isn't cool. "when [Integer, Integer]" won't match any integer
numbers :-(

> There was a somewhat recent thread about changing Array#=== to
> be an element-wise application of ===, which makes a lot of
> sense to me.

to me too.
--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
2010/1/28 Albert Schlef <albertschlef(a)gmail.com>:
> Gary Wright wrote:
>> On Jan 27, 2010, at 4:14 PM, Albert Schlef wrote:
>>
>>> Cool. Where is "Array#===" documented? I couldn't find it.
>> Unfortunately Array#=== is just the same as Array#==,
>
> So it isn't cool. "when [Integer, Integer]" won't match any integer
> numbers :-(
>
>> There was a somewhat recent thread about changing Array#=== to
>> be an element-wise application of ===, which makes a lot of
>> sense to me.
>
> to me too.

Even if that would not happen you could do this and benefit from a
more efficient approach because tests are constants:

def MultiCheck(*a)
def a.===(b)
zip(b) {|x,y| return false unless x === y}
true
end
a.freeze
end

irb(main):023:0> T1 = MultiCheck String
=> [String]
irb(main):024:0> T2 = MultiCheck Fixnum, String
=> [Fixnum, String]
irb(main):025:0> def overload(*a)
irb(main):026:1> case a
irb(main):027:2> when T1
irb(main):028:2> puts "a single string"
irb(main):029:2> when T2
irb(main):030:2> puts a[1] * a[0]
irb(main):031:2> else
irb(main):032:2* raise ArgumentError, "can't %p" % a
irb(main):033:2> end
irb(main):034:1> end
=> nil
irb(main):035:0> overload "foo"
a single string
=> nil
irb(main):036:0> overload 2, "foo"
foofoo
=> nil
irb(main):037:0> overload 2
ArgumentError: can't 2
from (irb):32:in `overload'
from (irb):37
from /opt/bin/irb19:12:in `<main>'
irb(main):038:0>

:-)

Note, the same approach also works with regular expressions as
elements in the MultiCheck Array.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Mido Peace on
Thx Alot For Help ;)
--
Posted via http://www.ruby-forum.com/.