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
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
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
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"
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.
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.
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).
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
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"
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>
from (irb):14
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.
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.