From: "list. rb" Date: 2007-07-04T09:21:02+09:00 Subject: Re: Using a block to surround a string? ------=_Part_108022_17903868.1183508461953 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline def around_string(string) yield.join(string) end puts around_string("abc") { ["before", "after"] } On 7/3/07, Pete Yandell wrote: > > Ivan Vega wrote: > > > around_string('mystring') {|b, a| b = 'before'; a = 'after';} > > You want: > > around_string('mystring') { ['before', 'after'] } > > > def around_string(string, &block) > > b, a = yield > > "#{b}#{string}#{a}" > > end > > And you wnat: > > def around_string(string) > b, a = yield > "#{b}#{string}#{a}" > end > > For a quick intro to blocks, see the five minute talk I gave on them > last week: > > http://www.cogentconsulting.com.au/screencasts/index.html > > A block, however, seems like a terrible way of doing this. Have you > considered using an options hash? > > around_string('mystring', :before => 'before', :after => 'after') > > def around_string(string, *options) > "#{options[:before]}#{string}#{options[:after]}" > end > > > Pete Yandell > http://notahat.com/ > > ------=_Part_108022_17903868.1183508461953--