Array#===

M

Martin DeMello

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_BackSpace
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_BackSpace]
 
M

Mark Hubbart

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
 
J

Joel VanderWerf

Martin said:
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_BackSpace
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_BackSpace]
.
.
.
when config[:delete_keys]
__etc__


martin

How about using the splat idiom?

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

Joel VanderWerf

Martin said:
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#===.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top