Default value of property

I have an object, with a method that returns another object, or nil under
certain circumstances. Is there a better way to arrange things than to have
to write

if object.property
object.property.method
else
default_value
end

every time? I can’t just do “object.property.method” because if
’object.property’ returns nil then it fails and tells me that nil doesn’t
have a ‘method’ method.

Tim Bates

···


tim@bates.id.au

Well… one way would be

class NilClass
def method
default_value
end
end

But many will frown on this, and it
only works for one case anyway…

Hal

···

----- Original Message -----
From: “Tim Bates” tim@bates.id.au
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, November 25, 2002 7:08 PM
Subject: Default value of property

[snip]

if object.property
object.property.method
else
default_value
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?

-a

···

On Tue, 26 Nov 2002, Tim Bates wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Sorry to reply to myself…

I did try this, which doesn’t work:

x = obj.property.meth rescue default_value

I’m not sure why it doesn’t work, and I think
it would be nice if it did.

Hal

···

----- Original Message -----
From: “Hal E. Fulton” hal9000@hypermetrics.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, November 25, 2002 7:24 PM
Subject: Re: Default value of property

Well… one way would be

class NilClass
def method
default_value
end
end

But many will frown on this, and it
only works for one case anyway…

Hal

Reading something like that reminds me of an idea (yeah, flame
me for not having searched the archives if it came up already)
of having an accessor to put an expression into the context of
previous expressions. That is,

object.property and .method or default_value,

where the leading dot sets the ‘method’ into the context of
‘object.property’. Mind you, not setting any variables or
so, but switch the context (in this case, into a class which
solely consists of object.property, kind of anonymous singleton)

Maybe also … for winding up one context (or the usual nothing
leading, write it out). Well, this is just a weird idea, without
any discussions about PRO and CON etc…

-Martin

PS: Happy flaming.

···

On Tue, Nov 26, 2002 at 10:44:50AM +0900, ahoward wrote:

[snipsnap]
how about

object.property and object.property.method or default_value

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
#<A:0x4026d994>
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?

···

On Tue, Nov 26, 2002 at 10:44:50AM +0900, ahoward wrote:

On Tue, 26 Nov 2002, Tim Bates wrote:

[snip]

if object.property
object.property.method
else
default_value
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?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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

This is fine for small scripts … I’ve done it myself. But I would
avoid it in reusable libraries where I don’t have control over name
conflicts in common system classes.

There is an alternative that is only a little more work and is more
scalable. It is called the Null Object Pattern.

First you create you own “Null” class. Define any methods you want to
have default behaviour for. E.g.

require ‘singleton’
class MyNullObject
include Singleton
def method
default_value
end
# More methods here as needed.
end

Then, just arrange your properties to be initialized to your special
“null” object.

class MyObject
attr_reader :property
def initialize
@property = MyNullObject.instance
end
end

Now, you never have to check to see if “property” is null or not. It
will always have some behavior (if only the default one). For example,
its usage is:

mo = MyObject.new
mo.property.method # no need for checking!

···

On Mon, 2002-11-25 at 20:24, Hal E. Fulton wrote:

Well… one way would be

class NilClass
def method
default_value
end
end

But many will frown on this, and it
only works for one case anyway…


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

rescue doesn’t normally catch NameErrors

this will work

/scratch > irb
irb(main):001:0> begin
irb(main):002:1* foo
irb(main):003:1> rescue NameError
irb(main):004:1> puts ‘did not define foo…’
irb(main):005:1> end
did not define foo…

this won’t

irb(main):006:0> begin
irb(main):007:1* foo
irb(main):008:1> rescue
irb(main):009:1> puts ‘did not define foo…’
irb(main):010:1> end
NameError: undefined local variable or method `foo’ for #Object:0x401dbce0 from (irb):7

this has confused me on several occasions, though it kind of make sense…

-a

···

On Tue, 26 Nov 2002, Hal E. Fulton wrote:

I did try this, which doesn’t work:

x = obj.property.meth rescue default_value

I’m not sure why it doesn’t work, and I think
it would be nice if it did.

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi –

···

On Wed, 27 Nov 2002, Mauricio [iso-8859-1] Fernández wrote:

On Tue, Nov 26, 2002 at 10:44:50AM +0900, ahoward wrote:

On Tue, 26 Nov 2002, Tim Bates wrote:

[snip]

if object.property
object.property.method
else
default_value
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

Or:

b.meth if b

(if I’m understanding this part of the discussion correctly…)

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

rescue doesn't normally catch NameErrors

This is changed in 1.7

pigeon% /usr/bin/ruby -e 'a = .to_str rescue ""; p a'
-e:1: undefined method `to_str' for :Array (NameError)
pigeon%

pigeon% ruby -ve 'a = .to_str rescue ""; p a'
ruby 1.7.3 (2002-11-17) [i686-linux]
""
pigeon%

Guy Decoux

[batsman:]

What’s wrong about ‘? :’ ?

irb(main):001:0> b = nil
nil
irb(main):002:0> (__f=b)?__f.meth : nil

Or:

b.meth if b

(if I’m understanding this part of the discussion correctly…)

Your code is equivalent to batsman’s, but the OP wanted a default value thrown
in, so:

b and b.meth or default_value

The original and the best!

David

Gavin

···

From: dblack@candle.superlink.net

Hi –

From: dblack@candle.superlink.net

[batsman:]

What’s wrong about ‘? :’ ?

irb(main):001:0> b = nil
nil
irb(main):002:0> (__f=b)?__f.meth : nil

Or:

b.meth if b

(if I’m understanding this part of the discussion correctly…)

Your code is equivalent to batsman’s, but the OP wanted a default value thrown
in, so:

b and b.meth or default_value

The original and the best!

You mean you don’t like:

(b || Class.new.module_eval {
def meth; “default value”; end; self
}.new) .meth

?? :slight_smile:

(It’s been a long week…)

David

···

On Wed, 27 Nov 2002, Gavin Sinclair wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi,

What’s wrong about ‘? :’ ?

irb(main):001:0> b = nil
nil
irb(main):002:0> (__f=b)?__f.meth : nil

Or:

b.meth if b

(if I’m understanding this part of the discussion correctly…)

Your code is equivalent to batsman’s, but the OP wanted a default value thrown
in, so:

b and b.meth or default_value

They are not equivalent when b.meth returns nil/false.

What about this? :slight_smile:

module Kernel; def not?; self; end; end
class FalseClass; def not?; yield; end; end
class NilClass; def not?; yield; end; end
catch(:property) {
object.property.not?{throw :property, default_value}.method
}

···

At Wed, 27 Nov 2002 07:59:47 +0900, Gavin Sinclair wrote:


Nobu Nakada

What was the reasoning behind this change?

Paul

···

On Tue, Nov 26, 2002 at 07:50:53PM +0900, ts wrote:

rescue doesn’t normally catch NameErrors

This is changed in 1.7

pigeon% /usr/bin/ruby -e ‘a = .to_str rescue “”; p a’
-e:1: undefined method `to_str’ for :Array (NameError)
pigeon%

pigeon% ruby -ve ‘a = .to_str rescue “”; p a’
ruby 1.7.3 (2002-11-17) [i686-linux]

pigeon%

I followed the instructions to install RUDL from the
site
http://froukepc.dhs.org/rudl/docs/howto_install.html

Opened sample RUDL program and tried to run it and
this is what I get. (Point to note is that RUDL.so is
indeed in C:/ruby/lib/ruby/1.7/i386-mswin32/RUDL.so)

C:/RUBY/lib/ruby/1.7/i386-mswin32/RUDL.so: 1157: One
of the library files needed to run this application
cannot be found. -
C:/RUBY/lib/ruby/1.7/i386-mswin32/RUDL.so (LoadError)
from framerate_test.rb:5

I guess there might be a difference between RUBY and
ruby from a now win32 perspective and that may be
causing problems. Anyone suceeded in getting RUDL to
work?

Thanks
Ramesh

···

=====
Rameshsharma Ramloll PhD
Assistive Technology & Neuroscience Research CenterNational Rehabilitation Hospital
102 Irving St., NW
Washington, DC 20010
202-877-1932 phone ext 1935
202-723-0628 fax
www.ramloll.com

What was the reasoning behind this change?

If I'm right [ruby-dev:12763], [ruby-dev:12767], etc

Guy Decoux

Hi,

I followed the instructions to install RUDL from the
site
http://froukepc.dhs.org/rudl/docs/howto_install.html

Opened sample RUDL program and tried to run it and
this is what I get. (Point to note is that RUDL.so is
indeed in C:/ruby/lib/ruby/1.7/i386-mswin32/RUDL.so)

C:/RUBY/lib/ruby/1.7/i386-mswin32/RUDL.so: 1157: One
of the library files needed to run this application
cannot be found. -

Wow, what a horrid error message. :frowning: Guess it could be worse -
I wrote a program in my youth that irritated people in the office
trying to use it by complaining, “Something went wrong.”

C:/RUBY/lib/ruby/1.7/i386-mswin32/RUDL.so (LoadError)
from framerate_test.rb:5

I guess there might be a difference between RUBY and
ruby from a now win32 perspective and that may be
causing problems. Anyone suceeded in getting RUDL to
work?

I have RUDL V0.6 running with:
ruby 1.6.6 (2001-12-26) [i586-mswin32]

Note that RUDL is packaged with a number of .dlls…
For me they unzipped into rudl-0.6/dll… They include
04/05/2001 05:24p 169,443 jpeg.dll
04/05/2001 05:24p 94,720 libpng1.dll
11/08/2001 09:31a 225,280 SDL.dll
01/26/2002 01:40p 90,112 SDL_gfx.dll
12/18/2001 12:53p 36,864 SDL_image.dll
12/18/2001 12:53p 258,048 SDL_mixer.dll
12/18/2001 12:53p 172,032 SDL_ttf.dll
11/11/2001 01:38a 204,800 smpeg.dll
07/27/2000 12:13a 53,760 zlib.dll
…and will need to be somewhere in your Windows executable
search path…

Thanks
Ramesh

Hope this helps,

Bill

···

From: “Ramesh Ramloll” ramloll2001@yahoo.com

Hi Bill,
Checked the windows excutables search path, its
pointing to the right place. All the dlls are in
c:\ruby\bin and the latter of course is on my PATH.
Well was just trying some interesting minimal
multi-media application to learn Ruby. I was
wondering about what I would like in a development
environment. I have been programming since my
undergrad years which is 10 yrs now, and from a
not-too skilled and impatient programmer’s
perspective, I think there has always been a lot of
un-necessary cognitive load on setting things up
before you get started. I remember this was true with
the Java CLASSPATH in the begining. I am familiar with
the VC++ environment, but I have too say linking with
other libraries etc… was never without any trials
and errors. Is it a worthwhile goal to inject some
intelligence in a dev environment that will allow some
piece of code to search for the appropriate libs,
modules automatically? May be there could be some dll
(and whatever else the program needs) recognition
facility inbuilt. I am just thinking aloud, am sure
other people have thought and built things around this
that I am not aware of. Anyway, at least OpenGL and
GLUT seems to work for me so may I drop RUDL for now.
R

···

— Bill Kelly billk@cts.com wrote:

Hi,

From: “Ramesh Ramloll” ramloll2001@yahoo.com

I followed the instructions to install RUDL from
the
site

http://froukepc.dhs.org/rudl/docs/howto_install.html

Opened sample RUDL program and tried to run it and
this is what I get. (Point to note is that RUDL.so
is
indeed in
C:/ruby/lib/ruby/1.7/i386-mswin32/RUDL.so)

C:/RUBY/lib/ruby/1.7/i386-mswin32/RUDL.so: 1157:
One
of the library files needed to run this
application
cannot be found. -

Wow, what a horrid error message. :frowning: Guess it could
be worse -
I wrote a program in my youth that irritated people
in the office
trying to use it by complaining, “Something went
wrong.”

C:/RUBY/lib/ruby/1.7/i386-mswin32/RUDL.so
(LoadError)
from framerate_test.rb:5

I guess there might be a difference between RUBY
and
ruby from a now win32 perspective and that may be
causing problems. Anyone suceeded in getting RUDL
to
work?

I have RUDL V0.6 running with:
ruby 1.6.6 (2001-12-26) [i586-mswin32]

Note that RUDL is packaged with a number of .dlls…
For me they unzipped into rudl-0.6/dll… They
include
04/05/2001 05:24p 169,443 jpeg.dll
04/05/2001 05:24p 94,720 libpng1.dll
11/08/2001 09:31a 225,280 SDL.dll
01/26/2002 01:40p 90,112 SDL_gfx.dll
12/18/2001 12:53p 36,864
SDL_image.dll
12/18/2001 12:53p 258,048
SDL_mixer.dll
12/18/2001 12:53p 172,032 SDL_ttf.dll
11/11/2001 01:38a 204,800 smpeg.dll
07/27/2000 12:13a 53,760 zlib.dll
…and will need to be somewhere in your Windows
executable
search path…

Thanks
Ramesh

Hope this helps,

Bill

=====
Rameshsharma Ramloll PhD
Assistive Technology & Neuroscience Research CenterNational Rehabilitation Hospital
102 Irving St., NW
Washington, DC 20010
202-877-1932 phone ext 1935
202-723-0628 fax
www.ramloll.com

Hi Ramesh,

Hi Bill,
Checked the windows excutables search path, its
pointing to the right place. All the dlls are in
c:\ruby\bin and the latter of course is on my PATH.
Well was just trying some interesting minimal
multi-media application to learn Ruby.

Sorry to hear RUDL isn’t working for you, but glad GLUT and
OpenGL are. Perhaps the win32 Dependency Walker
( http://www.dependencywalker.com/ ) would be of some utility
in troublshooting RUDL’s Missing Link…

Also, I’ve noticed on RAA an alternate SDL binding for Ruby,
but haven’t tried it yet myself:
http://www.ruby-lang.org/en/raa-list.rhtml?id=415

Additionally, in case it might be relevant to what you want
to do with Ruby, here’s one way (albeit windows-specific) to
play sounds:
http://www.ruby-talk.com/55423

I think there has always been a lot of
un-necessary cognitive load on setting things up
before you get started. I remember this was true with
the Java CLASSPATH in the begining.

Yes, it’s regrettable the RUDL library doesn’t specify which
DLL it’s unable to link with. . . . I haven’t experienced
many dependency-related problems in Ruby yet, so far most
things I’ve added have just Worked for me, but maybe I’ve
been lucky so far. I’ve certainly had my share of
NoClassDefFound exceptions in Java. :slight_smile:

Anyway, at least OpenGL and
GLUT seems to work for me so may I drop RUDL for now.

Good luck & Have fun with Ruby !

Regards,

Bill

···

From: “Ramesh Ramloll” ramloll2001@yahoo.com

Hi Bill,
Thanks for the dependency walker tool tip. I managed
to find out what the problem is.
The version of RUDL.so I have depends on
mswin32-ruby17.dll. In my installation of ruby which
is the ‘one click installation stuff from the
Pragmaticprogramming.com site’ I have
msvcrt-ruby17.dll
Has anyone got the proper dll which I could just try
to put in the ruby\bin folder? I found an
mswin32-ruby16.dll from somewhere but predictably got
a segmentation fault while trying to run the sample
examples.
Thanks again Bill, this tool is a great find.
R

···

— Bill Kelly billk@cts.com wrote:

Hi Ramesh,

From: “Ramesh Ramloll” ramloll2001@yahoo.com

Hi Bill,
Checked the windows excutables search path, its
pointing to the right place. All the dlls are in
c:\ruby\bin and the latter of course is on my
PATH.
Well was just trying some interesting minimal
multi-media application to learn Ruby.

Sorry to hear RUDL isn’t working for you, but glad
GLUT and
OpenGL are. Perhaps the win32 Dependency Walker
( http://www.dependencywalker.com/ ) would be of
some utility
in troublshooting RUDL’s Missing Link…

Also, I’ve noticed on RAA an alternate SDL binding
for Ruby,
but haven’t tried it yet myself:
http://www.ruby-lang.org/en/raa-list.rhtml?id=415

Additionally, in case it might be relevant to what
you want
to do with Ruby, here’s one way (albeit
windows-specific) to
play sounds:
http://www.ruby-talk.com/55423

I think there has always been a lot of
un-necessary cognitive load on setting things up
before you get started. I remember this was true
with
the Java CLASSPATH in the begining.

Yes, it’s regrettable the RUDL library doesn’t
specify which
DLL it’s unable to link with. . . . I haven’t
experienced
many dependency-related problems in Ruby yet, so far
most
things I’ve added have just Worked for me, but maybe
I’ve
been lucky so far. I’ve certainly had my share of
NoClassDefFound exceptions in Java. :slight_smile:

Anyway, at least OpenGL and
GLUT seems to work for me so may I drop RUDL for
now.

Good luck & Have fun with Ruby !

Regards,

Bill

=====
Rameshsharma Ramloll PhD
Assistive Technology & Neuroscience Research CenterNational Rehabilitation Hospital
102 Irving St., NW
Washington, DC 20010
202-877-1932 phone ext 1935
202-723-0628 fax
www.ramloll.com