From: "Mauricio Fernández" Date: 2003-02-01T05:38:54+09:00 Subject: Re: Local variables & blocks On Sat, Feb 01, 2003 at 02:06:36AM +0900, Robert Klemme wrote: > > Ok, I include another example to check whether I got it right: > > def blocktest(*args) > foo = "hello" > p foo > > args.each do |arg| > > # local assignment > arg = "<#{ arg }>" > p arg > > # creation and assignment to 'bar' in enclosing scope > if defined? bar > then > # append > bar.concat( arg.to_s ) > else > # initialization on first run > bar = arg.to_s > end > > foo = "world" > end > > # prints "world" if args was not empty, > # "hello" otherwise > p foo > > # prints a string containing all arguments > # as strings enclosed in <> if there were any > # otherwise raises an error... > p bar > end > > Since the initialization seems to be a bit verbose, I must have missed > something. At least the test for "defined?" strikes me as unrubyish... You can write (bar ||= '').concat arg.to_s or (bar ||= '') << arg if you want fewer keystrokes :) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com ...[Linux's] capacity to talk via any medium except smoke signals. -- Dr. Greg Wettstein, Roger Maris Cancer Center