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
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 W.
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 …
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
On Thu, 01 Dec 2005 13:40:51 -0000, James Edward G. II [email protected] 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…
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.
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.
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.