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.
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
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
====================================
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…
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
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…
/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
====================================
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?
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. 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…
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
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. 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…
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…
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.
Anyway, at least OpenGL and
GLUT seems to work for me so may I drop RUDL for now.
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
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…
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.
Anyway, at least OpenGL and
GLUT seems to work for me so may I drop RUDL for
now.