Require!

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

Boy do I feel stupid. :expressionless:

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 :slight_smile:

Thanks!

路路路

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"

Hi --

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

Boy do I feel stupid. :expressionless:

Don't. That's a pretty traditional rite of passage :slight_smile:

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 :slight_smile:

Interesting. The mind boggles at the side-effect and hard-to-find bug
possibilities :slight_smile: But it's quite cool, and I'd never known of it
before.

   class E
     include Enumerable
     def each
       3.times {|i| puts "Hello!"; yield i }
     end
   end

   def x(*args)
     p *args
   end

   x(*E.new)

:slight_smile:

David

路路路

On Thu, 1 Dec 2005, Ross Bamford wrote:

--
David A. Black
dblack@wobblini.net

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

T.

Actually * looks for a to_a.

James Edward Gray II

路路路

On Nov 30, 2005, at 7:52 PM, Ross Bamford wrote:

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 :slight_smile:

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

Gary Wright

路路路

On Nov 30, 2005, at 11:32 PM, Trans wrote:

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

:slight_smile: 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.

路路路

On Thu, 01 Dec 2005 04:30:28 -0000, Trans <transfire@gmail.com> wrote:

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

T.

--
Ross Bamford - rosco@roscopeco.remove.co.uk

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.

路路路

--
Lionel Thiry

Personal web site: http://users.skynet.be/lthiry/

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

:slight_smile: 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,

路路路

On Thu, 01 Dec 2005 13:40:51 -0000, James Edward Gray II <james@grayproductions.net> wrote:

On Nov 30, 2005, at 7:52 PM, Ross Bamford wrote:

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 :slight_smile:

Actually * looks for a to_a.

James Edward Gray II

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"

James Edward Gray II wrote:

路路路

On Nov 30, 2005, at 7:52 PM, Ross Bamford wrote:

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 :slight_smile:

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

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,

路路路

On Thu, 01 Dec 2005 05:45:53 -0000, <gwtmp01@mac.com> wrote:

On Nov 30, 2005, at 11:32 PM, Trans wrote:

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

--
Ross Bamford - rosco@roscopeco.remove.co.uk

Hi --

路路路

On Thu, 1 Dec 2005, Lionel Thiry wrote:

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.

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

--
David A. Black
dblack@wobblini.net

Hi --

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?

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

路路路

On Fri, 2 Dec 2005, Brian Buckley wrote:

--
David A. Black
dblack@wobblini.net

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.

路路路

On Thu, 01 Dec 2005 15:01:05 -0000, Brian Buckley <briankbuckley@gmail.com> wrote:

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

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"

When I kicked this thread off, I'd just done some quick tests that suggested it worked like above, and on anything with each, and I guessed that (since Enumerable defines to_a, and everything is in terms of each) that it must work that way.
I'm still pretty sure it does to_ary followed by to_a, but as others showed me, it doesn't actually work with just 'each'.

   class Clazz
     def each
       yield 1
       yield 2
       yield 3
     end
   end

   c = Clazz.new
   c = [*c]
   p c => [#<Clazz:0xb7f7ccdc>]

That's I think coming from the default to_a, and with the 1.9 snapshot I have it gives a TypeError ('Cannot convert Clazz into Array' - the default to_a is gone). It does _seem_ to work on anything with each if you include Enumerable, but that's because Enumerable defines everything (including to_a) in terms of 'each'. If you add 'include Enumerable' to the class definition above, you get [1,2,3] as expected.

路路路

On Fri, 02 Dec 2005 06:13:38 -0000, Hal Fulton <hal9000@hypermetrics.com> wrote:

James Edward Gray II wrote:

On Nov 30, 2005, at 7:52 PM, Ross Bamford wrote:

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 :slight_smile:

  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.

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"

James Edward Gray II wrote:

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 :slight_smile:

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.

It doesn't:

>> class NoArr
>> def each
>> yield 1
>> yield 2
>> yield 3
>> end
>> undef to_a
>> end
=> nil
>> def multi( one, two, three )
>> p one
>> p two
>> p three
>> end
=> nil
>> multi( *NoArr.new )
NoMethodError: undefined method `to_a' for #<NoArr:0x30d980>
         from (irb):14

James Edward Gray II

路路路

On Dec 2, 2005, at 12:13 AM, Hal Fulton wrote:

On Nov 30, 2005, at 7:52 PM, Ross Bamford wrote:

David A. Black a 茅crit :

Hi --

Trans a 锟絚rit :

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.

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.

路路路

On Thu, 1 Dec 2005, Lionel Thiry wrote:

--
Lionel Thiry

Personal web site: http://users.skynet.be/lthiry/

   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?

Brian Buckley

David A. Black wrote:

As Gary says it's actually to_a, not to_ary

#to_ary is called first; if it doesn't exist #to_a will be called.

Hi --

路路路

On Thu, 1 Dec 2005, Lionel Thiry wrote:

David A. Black a 茅crit :

Hi --

On Thu, 1 Dec 2005, Lionel Thiry wrote:

Trans a 锟絚rit :

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.

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.

Oh, *that* change :slight_smile: I forgot about that.

David

--
David A. Black
dblack@wobblini.net