From: Guillaume Marcais Date: 2004-02-01T07:11:22+09:00 Subject: Re: cascading message sends What about this: [gus@gusmac tmp]$ cat chain.rb #! /usr/bin/ruby -w class Chain def initialize(o) @o = o end def method_missing(msg, *args) @o.send(msg, *args) @o end end class Object def chain Chain.new(self) end end a = [0, 1, 2, 3] a.chain.delete_at(2).delete_at(1) p a [gus@gusmac tmp]$ ruby chain.rb [0, 3] Guillaume. Le 31 janv. 04, � 16:19, Mark J. Reed a �crit : > On Sat, Jan 31, 2004 at 11:16:50AM -0800, Paul MG wrote: >> In Smalltalk, which Ruby is much like in some ways, you can 'cascade' >> message sends. That is, call a bunch of methods on the same object >> without naming it explicitly over and over. Eg: >> >> aCollection add: 5; add: 7; add: 'foo' ! >> >> calls 'add' on 'aCollection' 3 times, sending 5, 7, and 'foo'. >> >> Does Ruby have this syntax? > > Others have mentioned a couple ways to do this generally, but > I just wanted to point out that you can do this without any > special syntax as long as the methods involved return `self' - > which many do, including Array#push: > > aCollection = [] > aCollection.push(5).push(7).push('foo') > [5, 7, "foo"] > > > Array#<< does, too, and long chains with it have less > syntactic clutter: > > > aCollection << 4 << 3 << 2 > [5, 7, "foo", 4, 3, 2] > > > > -Mark > >