require!

Discussion in 'Ruby' started by Ross Bamford, Dec 1, 2005.

  1. Ross Bamford

    Ross Bamford Guest

    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!

    --
    Ross Bamford -
    "\e[1;31mL"
     
    Ross Bamford, Dec 1, 2005
    #1
    1. Advertising

  2. Ross Bamford

    Trans Guest

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

    T.
     
    Trans, Dec 1, 2005
    #2
    1. Advertising

  3. Ross Bamford

    Guest

    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
     
    , Dec 1, 2005
    #3
  4. Ross Bamford

    Ross Bamford Guest

    On Thu, 01 Dec 2005 04:30:28 -0000, 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.
    >
    > 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.

    --
    Ross Bamford -
     
    Ross Bamford, Dec 1, 2005
    #4
  5. Ross Bamford

    Ross Bamford Guest

    On Thu, 01 Dec 2005 05:45:53 -0000, <> 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
    >


    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,

    --
    Ross Bamford -
     
    Ross Bamford, Dec 1, 2005
    #5
  6. Ross Bamford

    Lionel Thiry Guest

    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/
     
    Lionel Thiry, Dec 1, 2005
    #6
  7. --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 --

    On Thu, 1 Dec 2005, Lionel Thiry wrote:

    > Trans a =E9crit :
    >> 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

    --=20
    David A. Black

    --8323328-248990591-1133437847=:698--
    --8323328-248990591-1133437847=:698--
     
    David A. Black, Dec 1, 2005
    #7
  8. Ross Bamford

    Lionel Thiry Guest

    David A. Black a écrit :
    > 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
    >


    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.

    --
    Lionel Thiry

    Personal web site: http://users.skynet.be/lthiry/
     
    Lionel Thiry, Dec 1, 2005
    #8
  9. 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 :)


    Actually * looks for a to_a.

    James Edward Gray II
     
    James Edward Gray II, Dec 1, 2005
    #9
  10. > 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
     
    Brian Buckley, Dec 1, 2005
    #10
  11. Ross Bamford

    Ross Bamford Guest

    On Thu, 01 Dec 2005 13:40:51 -0000, 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 :)

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

    --
    Ross Bamford -
    "\e[1;31mL"
     
    Ross Bamford, Dec 1, 2005
    #11
  12. Hi --

    On Fri, 2 Dec 2005, Brian Buckley 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?


    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

    --
    David A. Black
     
    David A. Black, Dec 1, 2005
    #12
  13. Ross Bamford

    Ross Bamford Guest

    On Thu, 01 Dec 2005 15:01:05 -0000, Brian Buckley
    <> 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
    >
    >


    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.

    --
    Ross Bamford -
    "\e[1;31mL"
     
    Ross Bamford, Dec 1, 2005
    #13
  14. > 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
     
    Brian Buckley, Dec 1, 2005
    #14
  15. > But when might the 1st way (using the unary operator) be preferred to
    > the more explicit 2nd way?


    Oops. Scratch that last question.
     
    Brian Buckley, Dec 1, 2005
    #15
  16. Hi --

    On Fri, 2 Dec 2005, Brian Buckley wrote:

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

    --
    David A. Black
     
    David A. Black, Dec 1, 2005
    #16
  17. > 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.
     
    Brian Buckley, Dec 1, 2005
    #17
  18. 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.
     
    Daniel Schierbeck, Dec 1, 2005
    #18
  19. Hi --

    On Fri, 2 Dec 2005, Daniel Schierbeck wrote:

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


    Whoops -- I got a false negative on a (flawed) little test I ran.


    David

    --
    David A. Black
     
    David A. Black, Dec 1, 2005
    #19
  20. Ross Bamford

    Hal Fulton Guest

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

    >
    >
    > 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
     
    Hal Fulton, Dec 2, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herman Eldering

    Re: Why does using keyword require a new scope?

    Herman Eldering, Aug 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    970
    Herman Eldering
    Aug 24, 2003
  2. Jip from Paris
    Replies:
    0
    Views:
    1,899
    Jip from Paris
    Aug 25, 2003
  3. Don
    Replies:
    2
    Views:
    431
    Ralph Henneberg
    Dec 10, 2003
  4. Patrick LeBoutillier

    use/require handler?

    Patrick LeBoutillier, Jan 31, 2004, in forum: Perl
    Replies:
    4
    Views:
    491
    Gunnar Hjalmarsson
    Jan 31, 2004
  5. steffen staehle
    Replies:
    0
    Views:
    494
    steffen staehle
    Sep 7, 2004
Loading...

Share This Page