Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array#===

Reply
Thread Tools

Array#===

 
 
Martin DeMello
Guest
Posts: n/a
 
      02-19-2005
It seems a bit of a waste for Array#=== to default to Object#=== - it'd
be far more useful, IMO, to have it call include?. Can anyone see a
drawback to this?

Here's a real-world use case from the FXIrb code:

def onKeyPress(sender,sel,event)
case event.code
when Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSp ace
if getCursorPos > @anchor
super
end

which a change to Array#=== would let me write as

config[:delete_keys] =
[Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSp ace]
 
Reply With Quote
 
 
 
 
Mark Hubbart
Guest
Posts: n/a
 
      02-19-2005
On Sun, 20 Feb 2005 07:04:44 +0900, Martin DeMello
<(E-Mail Removed)> wrote:
> It seems a bit of a waste for Array#=== to default to Object#=== - it'd
> be far more useful, IMO, to have it call include?. Can anyone see a
> drawback to this?


I've wondered about this too. It seems that the === method loosely
translates to a membership test, so it would make sense if it tested
membership with arrays, as well as ranges and classes and regexps. I
can't see any drawbacks, though. (though that doesn't mean there
aren't any)

cheers,
Mark


 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-20-2005
Martin DeMello wrote:
> It seems a bit of a waste for Array#=== to default to Object#=== - it'd
> be far more useful, IMO, to have it call include?. Can anyone see a
> drawback to this?
>
> Here's a real-world use case from the FXIrb code:
>
> def onKeyPress(sender,sel,event)
> case event.code
> when Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSp ace
> if getCursorPos > @anchor
> super
> end
>
> which a change to Array#=== would let me write as
>
> config[:delete_keys] =
> [Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSp ace]
> .
> .
> .
> when config[:delete_keys]
> __etc__
>
>
> martin


How about using the splat idiom?

a = [1,2,3]
case 3
when *a; p a
end


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-20-2005
Martin DeMello wrote:
> It seems a bit of a waste for Array#=== to default to Object#=== - it'd
> be far more useful, IMO, to have it call include?. Can anyone see a
> drawback to this?


We'd lose this, FWIW:

points = [
[1,2],
[3,4],
[0,0]
]

points.each do |point|
case point
when [0,0]
puts "origin!"
end
end

To be a bit philosophical, I'd say that Arrays, unlike Regexps, do not
exist primarily to match, and so #=== should be Object#===.


 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      02-20-2005
Joel VanderWerf <(E-Mail Removed)> wrote:

> How about using the splat idiom?
>
> a = [1,2,3]
> case 3
> when *a; p a
> end


Very neat - didn't think of that.

martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments