Hi,
I’m quite new to Ruby, so please excuse me if this question is a bit
silly.
Consider:
class Foo
def initialize
@a = []
end
def bar§
@a << p
end
end
It’s nice that Ruby returns the last evaluated expression, without
putting an explicit return. However, Foo#bar dangerously exposes the
Foo class internals.
My question is, what is the common idiom to avoid this? Simply adding
a return nil? def bar§; @a << p; return nil; end
Thanks in advance
2007/11/9, [email protected] [email protected]:
end
My question is, what is the common idiom to avoid this? Simply adding
a return nil? def bar(p); @a << p; return nil; end
In this case I’d do
def bar(x)
@a << x
self
end
Because then you can nicely chain calls to #bar. Depending on
circumstances you might even consider implementing #<< instead of /
additionally to #bar.
Kind regards
robert
On Nov 9, 2007, at 09:41 , Robert K. wrote:
2007/11/9, [email protected]
[email protected]:
a return nil? def bar(p); @a << p; return nil; end
In this case I’d do
def bar(x)
@a << x
self
end
Follow this advice.
use.inject do |as, often| as.you_can - without end
Ignore this advice.
Ok, thanks a lot for the tips.