Having a block executed in the context of an instance

Anthony DeRobertis [email protected] writes:

meth . attr = 42

and other fun variations :frowning:

It’s a bit iffy, isn’t it? Wouldn’t it also break Matz’s golden
rules of when to use call parens? I believe it goes:

  • if there are no args, don’t use parens.
  • elsif you use the return value, use parens.
  • else parens are optional.

Well I found out either way I go with passing a block to Button.new(),
it
doesn’t work very well because subclasses’ initialize() is called
afterwards
and clobbers the changes :slight_smile: So I just have another method called
init().


def init()
yield self
return self
end

button = Button.new.init do |b|
b.caption = ‘Click Me!’
end

Mike

On Thu, 09 Mar 2006 03:58:07 +0100, [email protected] wrote:

obj.x

or

obj(.x)

I would say it should be obj.x, as I didn’t propose to allow .meth as a
general shortcut for self.meth, but only when it is a left hand side of
an
asignment.

But this leads to:

meth .attr=42

I would say that would also be meth.attr=42 and not meth(.attr=42). If
you
want the latter, then use brackets.

Dominik