Forum: Ruby require!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
rosco (Guest)
on 2005-12-01 02:54
(Received via mailing list)
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!
dblack (Guest)
on 2005-12-01 04:47
(Received via mailing list)
Hi --

On Thu, 1 Dec 2005, Ross Bamford wrote:

> It (finally) clicked now, 'require' is just a method...
>
> Boy do I feel stupid. :|

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

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

Interesting.  The mind boggles at the side-effect and hard-to-find bug
possibilities :-)  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)

:-)


David
transfire (Guest)
on 2005-12-01 05:35
(Received via mailing list)
Hi Ross, actually as Florian recently taught me, *obj works for any
object with #to_ary defined --if that is what you mean.

T.
gwtmp01 (Guest)
on 2005-12-01 06:48
(Received via mailing list)
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
rosco (Guest)
on 2005-12-01 09:38
(Received via mailing list)
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.
>

:) 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.
rosco (Guest)
on 2005-12-01 09:50
(Received via mailing list)
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
>

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,
lthiryidontwantspams (Guest)
on 2005-12-01 12:44
(Received via mailing list)
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/
58aa8536f985277ebef53fa931863a3e?d=identicon&s=25 James G. (bbazzarrakk)
on 2005-12-01 14:43
(Received via mailing list)
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
8d9ed5a28d814f67121afb1cd49e64d5?d=identicon&s=25 Brian B. (brian)
on 2005-12-01 16:04
(Received via mailing list)
> 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
dblack (Guest)
on 2005-12-01 16:17
(Received via mailing list)
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
rosco (Guest)
on 2005-12-01 16:21
(Received via mailing list)
On Thu, 01 Dec 2005 13:40:51 -0000, James Edward Gray II
<james@grayproductions.net> wrote:

>
:) 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,
8d9ed5a28d814f67121afb1cd49e64d5?d=identicon&s=25 Brian B. (brian)
on 2005-12-01 16:29
(Received via mailing list)
>    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
rosco (Guest)
on 2005-12-01 16:29
(Received via mailing list)
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
>
>

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.
8d9ed5a28d814f67121afb1cd49e64d5?d=identicon&s=25 Brian B. (brian)
on 2005-12-01 16:33
(Received via mailing list)
> But when might the 1st way (using the unary operator) be preferred to
> the more explicit 2nd way?

Oops.  Scratch that last question.
dblack (Guest)
on 2005-12-01 16:33
(Received via mailing list)
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
8d9ed5a28d814f67121afb1cd49e64d5?d=identicon&s=25 Brian B. (brian)
on 2005-12-01 16:54
(Received via mailing list)
> 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.
hal9000 (Guest)
on 2005-12-02 07:17
(Received via mailing list)
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
rosco (Guest)
on 2005-12-02 09:44
(Received via mailing list)
On Fri, 02 Dec 2005 06:13:38 -0000, Hal Fulton
<hal9000@hypermetrics.com>
wrote:

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

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.
58aa8536f985277ebef53fa931863a3e?d=identicon&s=25 James G. (bbazzarrakk)
on 2005-12-02 14:46
(Received via mailing list)
On Dec 2, 2005, at 12:13 AM, Hal Fulton wrote:

> 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
This topic is locked and can not be replied to.