# Require!

**URL:** <https://rubytalk.org/t/require/22896>\
**Category:** ruby-talk\
**Created:** [1 December 2005 01:52 UTC](https://rubytalk.org/t/require/22896 "2005-12-01T01:52:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [1 December 2005 01:52 UTC](https://rubytalk.org/t/require/22896/1 "2005-12-01T01:52:30Z")

</div>

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 - [rosco@roscopeco.remove.co.uk](mailto:rosco@roscopeco.remove.co.uk)  
> "\e[1;31mL"

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [1 December 2005 03:44 UTC](https://rubytalk.org/t/require/22896/2 "2005-12-01T03:44:32Z")

</div>

Hi --

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

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

&nbsp;&nbsp;&nbsp;def x(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p \*args  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;x(\*E.new)

🙂

David

> **···**
>
> On Thu, 1 Dec 2005, Ross Bamford wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [1 December 2005 04:32 UTC](https://rubytalk.org/t/require/22896/3 "2005-12-01T04:32:31Z")

</div>

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

T.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [1 December 2005 13:40 UTC](https://rubytalk.org/t/require/22896/4 "2005-12-01T13:40:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [1 December 2005 05:45 UTC](https://rubytalk.org/t/require/22896/5 "2005-12-01T05:45:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [1 December 2005 08:37 UTC](https://rubytalk.org/t/require/22896/6 "2005-12-01T08:37:31Z")

</div>

🙂 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

---

<div class="post-metadata">

**Author:** ![Lionel\_Thiry3](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lionel\_Thiry3](https://rubytalk.org/u/Lionel_Thiry3)\
**Post date:** [1 December 2005 11:42 UTC](https://rubytalk.org/t/require/22896/7 "2005-12-01T11:42:31Z")

</div>

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/](http://users.skynet.be/lthiry/)

---

<div class="post-metadata">

**Author:** ![Brian\_Buckley](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Brian\_Buckley](https://rubytalk.org/u/Brian_Buckley)\
**Post date:** [1 December 2005 15:01 UTC](https://rubytalk.org/t/require/22896/8 "2005-12-01T15:01:05Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [1 December 2005 15:17 UTC](https://rubytalk.org/t/require/22896/9 "2005-12-01T15:17:31Z")

</div>

🙂 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 🙂
> > 
> > Actually \* looks for a to\_a.
> > 
> > James Edward Gray II
> 
> --  
> Ross Bamford - rosco@roscopeco.remove.co.uk  
> "\e[1;31mL"

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [2 December 2005 06:13 UTC](https://rubytalk.org/t/require/22896/10 "2005-12-02T06:13:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [1 December 2005 08:47 UTC](https://rubytalk.org/t/require/22896/11 "2005-12-01T08:47:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [1 December 2005 11:50 UTC](https://rubytalk.org/t/require/22896/12 "2005-12-01T11:50:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [1 December 2005 15:15 UTC](https://rubytalk.org/t/require/22896/13 "2005-12-01T15:15:30Z")

</div>

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:

&nbsp;&nbsp;&nbsp;def x(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p args  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;a = [1,2,3]  
&nbsp;&nbsp;&nbsp;x(a) # [[1,2,3,]]  
&nbsp;&nbsp;&nbsp;x(\*a) # [1,2,3]

David

> **···**
>
> On Fri, 2 Dec 2005, Brian Buckley wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [1 December 2005 15:27 UTC](https://rubytalk.org/t/require/22896/14 "2005-12-01T15:27:31Z")

</div>

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

&nbsp;&nbsp;def amethod(one,two,three)  
&nbsp;&nbsp;&nbsp;&nbsp;puts one, two, three  
&nbsp;&nbsp;end

If you call that with:

&nbsp;&nbsp;amethod((1..3).to\_a)

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

&nbsp;&nbsp;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"

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [2 December 2005 08:42 UTC](https://rubytalk.org/t/require/22896/15 "2005-12-02T08:42:34Z")

</div>

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

&nbsp;&nbsp;&nbsp;class Clazz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def each  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;c = Clazz.new  
&nbsp;&nbsp;&nbsp;c = [\*c]  
&nbsp;&nbsp;&nbsp;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 🙂
> > > 
> > > &nbsp;&nbsp;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"

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [2 December 2005 13:43 UTC](https://rubytalk.org/t/require/22896/16 "2005-12-02T13:43:09Z")

</div>

> 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 🙂
> > 
> > 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\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:

---

<div class="post-metadata">

**Author:** ![Lionel\_Thiry3](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lionel\_Thiry3](https://rubytalk.org/u/Lionel_Thiry3)\
**Post date:** [1 December 2005 12:17 UTC](https://rubytalk.org/t/require/22896/17 "2005-12-01T12:17:32Z")

</div>

David A. Black a écrit :

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

> **···**
>
> > On Thu, 1 Dec 2005, Lionel Thiry wrote:
> 
> --  
> Lionel Thiry
> 
> Personal web site: [http://users.skynet.be/lthiry/](http://users.skynet.be/lthiry/)

---

<div class="post-metadata">

**Author:** ![Brian\_Buckley](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Brian\_Buckley](https://rubytalk.org/u/Brian_Buckley)\
**Post date:** [1 December 2005 15:25 UTC](https://rubytalk.org/t/require/22896/18 "2005-12-01T15:25:58Z")

</div>

> &nbsp;&nbsp;&nbsp;a = [1,2,3]  
> &nbsp;&nbsp;&nbsp;x(a) # [[1,2,3,]]  
> &nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [1 December 2005 17:17 UTC](https://rubytalk.org/t/require/22896/19 "2005-12-01T17:17:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [1 December 2005 12:20 UTC](https://rubytalk.org/t/require/22896/20 "2005-12-01T12:20:02Z")

</div>

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 �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.
> 
> Oh, \*that\* change 🙂 I forgot about that.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

[Next page](https://rubytalk.org/t/require/22896.md?page=2)
