From: "Mauricio Fernández" Date: 2002-11-27T04:26:31+09:00 Subject: Re: Default value of property On Tue, Nov 26, 2002 at 10:44:50AM +0900, ahoward wrote: > On Tue, 26 Nov 2002, Tim Bates wrote: > > [snip] > T> if object.property > T> object.property.method > T> else > T> default_value > T> end > [snip] > > how about > > object.property and object.property.method or default_value > > you could do something like > > object.property.method rescue default_value > > but this does not catch NameError, unless someone else knows the syntax for > this? What's wrong about '? :' ? irb(main):001:0> b = nil nil irb(main):002:0> (__f=b)?__f.meth : nil nil irb(main):003:0> class A irb(main):004:1> def meth irb(main):005:2> puts "AAAA" irb(main):006:2> end irb(main):007:1> end nil irb(main):008:0> b = A.new # irb(main):009:0> (__f=b)?__f.meth : nil AAAA nil OK, we pollute the local variable name-space w/ __foo, but would anybody _really_ want to use that one? -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| 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