Messages to nil

Posted: 11-11-2003, 01:17 PM
Hi,

One of the things I got accustomed to in Objective C was nil's behavior as a
universal message sink: You could send nil anything, and you'd get nil
back.

No big deal, really, except that it made message cascades (which in Ruby
would look like Java- or OGNL-style keypaths) a bit easier to code, without
having to stop off at every point in the path and use an if-statement to see
if there was a real object still listening. When I switched over to
predominately using Java a few years back, it took me a couple of painful
days to change that habit (not to mention remembering to use "null"
instead...literally the most besetting annoyance I inflicted on myself in
trying to learn Java).

Just curious: would it be worth petitioning the Power That Be for a global
switch that induced that kind of behavior in nil? Would there be any
catastrophic consequences? (Please be gentle; I still have only the
faintest clue what I'm doing with Ruby, except enjoying it a great deal...)

- dan



Reply With Quote

Responses to "Messages to nil"

ts
Guest
Posts: n/a
 
Re: Messages to nil
Posted: 11-11-2003, 01:36 PM
>>>>> "d" == dhtapp <dhtapp@cox.net> writes:

d> Just curious: would it be worth petitioning the Power That Be for a global
d> switch that induced that kind of behavior in nil? Would there be any
d> catastrophic consequences?

Well, there were some related discussion about a Null pattern, see for
example

http://www.ruby-talk.org/cgi-bin/vfr...37?17391-25625
http://www.ruby-talk.org/cgi-bin/vfr...21?17655-25795

(these previous URL use frame)

http://www.ruby-talk.org/17785


--

Guy Decoux
Reply With Quote
Reimer Behrends
Guest
Posts: n/a
 
Re: Messages to nil
Posted: 11-11-2003, 01:51 PM
dhtapp (dhtapp@cox.net) wrote:
[...]
> Just curious: would it be worth petitioning the Power That Be for a global
> switch that induced that kind of behavior in nil? Would there be any
> catastrophic consequences? (Please be gentle; I still have only the
> faintest clue what I'm doing with Ruby, except enjoying it a great deal...)
Does

def nil.method_missing(*args) nil end

do what you want?

Reimer Behrends
Reply With Quote
Robert Klemme
Guest
Posts: n/a
 
Re: Messages to nil
Posted: 11-11-2003, 01:55 PM

"dhtapp" <dhtapp@cox.net> schrieb im Newsbeitrag
news:2c6sb.12896$7B2.12145@fed1read04...
> Hi,
>
> One of the things I got accustomed to in Objective C was nil's behavior
as a
> universal message sink: You could send nil anything, and you'd get nil
> back.
>
> No big deal, really, except that it made message cascades (which in Ruby
> would look like Java- or OGNL-style keypaths) a bit easier to code,
without
> having to stop off at every point in the path and use an if-statement to
see
> if there was a real object still listening. When I switched over to
> predominately using Java a few years back, it took me a couple of
painful
> days to change that habit (not to mention remembering to use "null"
> instead...literally the most besetting annoyance I inflicted on myself
in
> trying to learn Java).
>
> Just curious: would it be worth petitioning the Power That Be for a
global
> switch that induced that kind of behavior in nil? Would there be any
> catastrophic consequences? (Please be gentle; I still have only the
> faintest clue what I'm doing with Ruby, except enjoying it a great
deal...)

Well, instead of checking at each point you can simply catch
NoMethodError:

begin
"foo".bar.baz.foo.doit( "yes" )
rescue NoMethodError => e
puts "ERROR in method #{e.name}( #{e.args.inspect} )"
end

Btw: the same works in Java, only there you'd probably catch
NullPointerException.

Regards

robert

Reply With Quote
dhtapp
Guest
Posts: n/a
 
Re: Messages to nil
Posted: 11-11-2003, 02:05 PM
Wow, apparently so! Thanks!

- dan

"Reimer Behrends" <behrends@cse.msu.edu> wrote in message
news:slrnbr1tqv.kts.behrends@ellington.cse.msu.edu ...
>
> def nil.method_missing(*args) nil end
>
> do what you want?
>
> Reimer Behrends

Reply With Quote
 
LinkBack Thread Tools Search this Thread Display Modes
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Time Messages bluezboy Macromedia Dynamic HTML 2 02-20-2006 05:43 PM
Pop up messages Orla webforumsuser@macromedia.com Macromedia Director Basics 10 07-21-2003 11:23 PM
messages Ray Smith Windows Setup, Administration & Security 0 07-14-2003 02:15 PM
Pop-up messages Margaret Windows Setup, Administration & Security 1 07-13-2003 02:59 PM
IP Messages Matt Roper Windows Setup, Administration & Security 1 07-07-2003 03:49 AM