From: Joel VanderWerf Date: 2006-10-26T13:50:16+09:00 Subject: Re: Pass block instead of here document? Rick DeNatale wrote: ... > But in Smalltalk if you write: > > obj msg1;msg2 > > the object referenced by obj is sent msg1 and then msg2 in sequence, > regardless of the value returned by obj msg1. Not quite the same, but this is something similar that's been floating around the ml for a while: class Object def then yield(self) self end end hash = Hash.new. then do |h| h[1] = 2 end. then do |h| h[3] = 4 end p hash That's much clunkier syntactically, but it is actually useful in one case: when working with classes whose #new method doesn't take a block that allows further configuration of the new object. For example, sockets: def socket @socket ||= UDPSocket.open.then do |s| s.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true) s.connect("192.168.255.255", 1234) end end It works particularly well with the ||= assignment operator. Without the #then method you can't use ||= and it's just a little clunkier: def socket unless @socket @socket = UDPSocket.open @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true) @socket.connect("255.255.255.255", 1234) end @socket end -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407