From: Eric Hodel Date: 2005-01-20T03:45:04+09:00 Subject: Re: is defined? fast? --Apple-Mail-16--995960007 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On 19 Jan 2005, at 09:46, Robert Klemme wrote: > > "Eric Hodel" schrieb im Newsbeitrag > news:12DE17DB-69B1-11D9-AFB0-000D93436DA0@segment7.net... > > Looks like the simple var check is better performance wise. > Personally I > never felt the need for defined? - only during my initial Ruby stages, > when I carried some Perl thinking with me; but never after I got rid of > that. :-) module M def functionality do_stuff if @x end end class C include M end C.new.functionality With ruby -w, this will give an 'uninitialized instance variable' warning. M#functionality needs to be written this way to get rid of the warning: module M def functionality @x = default unless defined? @x do_stuff if @x end end > Ah, and btw, you don't need the "true if ". Not that it matters > performance wise since you applied it everywhere - but I prefer to > reduce > redundancy... :-) I feel it is necessary. You're benchmarking the case of: statement if defined? something vs. statement if $global I like being specific. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Apple-Mail-16--995960007 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFB7qqDMypVHHlsnwQRAqHsAJ9Xy4dnzr95vk71Yl+IHgsxLTjdMgCfTn5h N3CZl9ogw+NkmFLLn73FIU0= =ZtyN -----END PGP SIGNATURE----- --Apple-Mail-16--995960007--