On Sat, Aug 24, 2013 at 11:39 PM, Marc Heiler <lists / ruby-forum.com> wrote:

> "A cascade sends multiple messages to the same receiver"
>
> Hi, In smalltalk this is possible.
>
> As far as I know ruby has no direct way to support cascading method
> calls.
>
> object.method1().method2() # would not work as method1() would not
> return self
>
> Then I was thinking, perhaps with a different syntax:
>
> object.(method1|method2)
>
> Could return an array.
>
> Smalltalk has something like this:
>
> "If a series of messages are sent to the same receiver, they can also be
> written as a cascade with individual messages separated by semicolons:"
>
> Smalltalk Code:
>
>   Window new
>     label: 'Hello';
>     open
>
> "This rewrite of the earlier example as a single expression avoids the
> need to store the new window in a temporary variable. According to the
> usual precedence rules, the unary message "new" is sent first, and then
> "label:" and "open" are sent to the answer of "new"."
>
> This all works without one having to create a variable where Window.new
> is stored.
>
> In Pseudo-Ruby this could look like:
>
> Window.new.label('Hello').open # but of course this would not work
>

Window.new.instance_eval do
  label 'Hello'
  open
end

will work. Ruby/Tk uses a similar technique by allowing to pass a block to
the constructor of objects which will be invoked via #instance_eval.
 Examples:

http://rubylearning.com/satishtalim/ruby_tk_tutorial.html

Cheers

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/