require!

R

Ross Bamford

It (finally) clicked now, 'require' is just a method...

Boy do I feel stupid. :|

On another note, though, I also found out that calling amethod(*args)
works with anything with 'each' (any Enumerable?), which made me smile. It
was the post about ranges that made me get that :)

Thanks!
 
T

Trans

Hi Ross, actually as Florian recently taught me, *obj works for any
object with #to_ary defined --if that is what you mean.

T.
 
G

gwtmp01

Hi Ross, actually as Florian recently taught me, *obj works for any
object with #to_ary defined --if that is what you mean.

Actually, I think the * operator looks for #to_a not #to_ary.

Gary Wright
 
R

Ross Bamford

Hi Ross, actually as Florian recently taught me, *obj works for any
object with #to_ary defined --if that is what you mean.

T.

:) I see... I was pretty happy when I realised it did it at all, and of
course it makes complete sense with hindsight. Thanks for pointing the way.
 
R

Ross Bamford

Actually, I think the * operator looks for #to_a not #to_ary.

Gary Wright

Some quick experiments just now suggest it looks first for to_ary, then
to_a. As I say, I was pretty pleased to find it did it at all so I didn't
carry on playing with it ...

Cheers,
 
L

Lionel Thiry

Trans a écrit :
Hi Ross, actually as Florian recently taught me, *obj works for any
object with #to_ary defined --if that is what you mean.

T.

AFAIK may change in ruby2.
 
D

David A. Black

--8323328-248990591-1133437847=:698
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-248990591-1133437847=:698"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-248990591-1133437847=:698
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Trans a =E9crit :

AFAIK may change in ruby2.

As Gary says it's actually to_a, not to_ary -- and I think it will
probably still work in 2.x, because Enumerable#to_a is explicitly
defined (not the soon-to-disappear default to_a for all objects).


David

--=20
David A. Black
(e-mail address removed)
--8323328-248990591-1133437847=:698--
--8323328-248990591-1133437847=:698--
 
L

Lionel Thiry

David A. Black a écrit :
Hi --




As Gary says it's actually to_a, not to_ary -- and I think it will
probably still work in 2.x, because Enumerable#to_a is explicitly
defined (not the soon-to-disappear default to_a for all objects).


David

Mmm, I remember Matz wonders about possible changes in the way the array
expansion operator * could work. He wonders if he should differentiate
array [a,b,c] and list a,b,c or not. Until decided, I keep in mind that
the way *args works may change in ruby2.
 
J

James Edward Gray II

On another note, though, I also found out that calling amethod
(*args) works with anything with 'each' (any Enumerable?), which
made me smile. It was the post about ranges that made me get that :)

Actually * looks for a to_a.

James Edward Gray II
 
B

Brian Buckley

Actually * looks for a to_a.

Does calling amethod(*x) have the same meaning as calling
amethod(x.to_a)? If is it what is the benefit of using the (less
clear) first way?

Brian Buckley
 
R

Ross Bamford

Actually * looks for a to_a.

James Edward Gray II

:) I was pretty surprised it worked for non-arrays to be honest, so I
didn't dare imagine it might be even _more_ flexible. Ruby just keeps on
getting better the more I get to know it - I've not been so pleasantly
surprised, so often, for a very long time...

Cheers,
 
D

David A. Black

Hi --

Does calling amethod(*x) have the same meaning as calling
amethod(x.to_a)? If is it what is the benefit of using the (less
clear) first way?

They're not the same. * is actually a unary unnary operator:

def x(*args)
p args
end

a = [1,2,3]
x(a) # [[1,2,3,]]
x(*a) # [1,2,3]


David
 
R

Ross Bamford

Does calling amethod(*x) have the same meaning as calling
amethod(x.to_a)? If is it what is the benefit of using the (less
clear) first way?

Brian Buckley

It 'unwraps' a supplied [anything] to multiple declared arguments. Imagine:

def amethod(one,two,three)
puts one, two, three
end

If you call that with:

amethod((1..3).to_a)

you'll get an error, because you only supplied one argument - the array.
If you specify the '*', however:

amethod(*(1..3))

The range first gets converted to an array with to_a, and then that is
expanded into the method's argument list.

You can double-check by specifying either 1..2 or 1..4, which gives an
ArgumentError.
 
B

Brian Buckley

a =3D [1,2,3]
x(a) # [[1,2,3,]]
x(*a) # [1,2,3]

x(*a) # [1,2,3]
x(a.to_a) # also [1,2,3]

But when might the 1st way (using the unary operator) be preferred to
the more explicit 2nd way?

Brian Buckley
 
D

David A. Black

Hi --

a = [1,2,3]
x(a) # [[1,2,3,]]
x(*a) # [1,2,3]

x(*a) # [1,2,3]
x(a.to_a) # also [1,2,3]

But when might the 1st way (using the unary operator) be preferred to
the more explicit 2nd way?

I did leave off the to_a, though array.to_a is a no-op so it shouldn't
matter.

More to the point: what version of Ruby are you using? Here's the
output with 1.8.3:

$ cat args2.rb
def x(*args)
p args
end

a = [1,2,3]
x(a.to_a)
x(*a)

$ ruby -v args2.rb
ruby 1.8.3 (2005-09-21) [powerpc-darwin8.3.0]
[[1, 2, 3]]
[1, 2, 3]


David
 
B

Brian Buckley

More to the point: what version of Ruby are you using? Here's the
output with 1.8.3:

I am an older version (1.8.2) but I am getting the same result as you.
I didn't realize you'd intentionally omitted to noop to_a. That and
with Ross's explanation, now I get it.
 
H

Hal Fulton

James said:
Actually * looks for a to_a.

I always thought it looked for to_ary... now someone says it looks
first for to_ary, then to_a.

And I had no idea that it would work with just #each defined.

"Curiouser and curiouser," said Alice.


Hal
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top