Behaviour of nil

Hi,

why the ‘+’ method of nil isn’t defined ?

For exemple:
nil + “aaa” should return "aaa"
nil + 3 should return 3
nil + [1,2] should return [1,2] …
Perl works like that (at least for the first two exemple).
It is convenient to do something like
while gets do
res += $_
end

without having to do res = “” before.

Regards.

···


Cedric Foll
courriel: cedric dot foll at laposte dot net

Cédric Foll wrote:

Hi,

why the ‘+’ method of nil isn’t defined ?

For exemple:
nil + “aaa” should return “aaa”
nil + 3 should return 3
nil + [1,2] should return [1,2] …
Perl works like that (at least for the first two exemple).
It is convenient to do something like
while gets do
res += $_
end

without having to do res = “” before.

Regards.


Cedric Foll
courriel: cedric dot foll at laposte dot net

The problem here is that nil + “aaa” doesn’t work the same way as in
Perl, Ruby is an OO language and does things differently.

Here’s what happens.

  1. ‘nil’ is an object of the type NilClass.
  2. The ‘+’ is taken to be a method call on ‘nil’ with arguments of “aaa”.
  3. ‘nil’ has no such method ‘+’
  4. ‘nil’ raises an exception over missing method ‘+’

Something similar happens with fred + “aaa” except here ‘fred’ is a
reference to an object that at this point references no object, it
therefore has no methods and doesn’t know how to do ‘+’. So it too
raises an exception.

When you do fred = “” first you make ‘fred’ a reference to a String
object which does have a ‘+’ method and so fred + “aaa” works.

fred + “aaa” might look the same as $fred + “aaa” but the mechanics of
the two languages are very much different.

Hi,

why the ‘+’ method of nil isn’t defined ?

For exemple:
nil + “aaa” should return “aaa”
nil + 3 should return 3
nil + [1,2] should return [1,2] …
Perl works like that (at least for the first two exemple).
It is convenient to do something like
while gets do
res += $_
end

without having to do res = “” before.

Regards.


Cedric Foll
courriel: cedric dot foll at laposte dot net

Some people will give you good reasons why it’s not like that in Ruby. You
should try implementing it yourself and see if it causes you any problems down
the track.

class NilClass
def +(other)
other
end
end

Cheers,
Gavin

···

From: “Cédric Foll” cedric.foll-nospam@bigfoot.com

“Cédric Foll” cedric.foll-nospam@bigfoot.com writes:

why the ‘+’ method of nil isn’t defined ?

The current behavior is a useful way to catch programming errors such
as

@sum = @smu + 1

(along with other, more subtle, problems).

Cheers

Dave

Cédric Foll wrote:

Here’s what happens.

  1. ‘nil’ is an object of the type NilClass.
  2. The ‘+’ is taken to be a method call on ‘nil’ with arguments of
    “aaa”.
  3. ‘nil’ has no such method ‘+’
  4. ‘nil’ raises an exception over missing method ‘+’

I think he understood that; that’s why he asked:

···

— Peter Hickman peter@semantico.com wrote:

why the ‘+’ method of nil isn’t defined ?

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

The current behavior is a useful way to catch programming errors such
as

@sum = @smu + 1

This can be tracked down if all fields must be explicitly defined.

Gergo
±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

Michael Campbell wrote:

I think he understood that; that’s why he asked:

However he also stated that it worked in Perl which to me shows that he
has not thinking in terms of object / method / reciever but in terms of
type coercion of a non OO language.

@sum = @smu + 1

This can be tracked down if all fields must be explicitly defined.

What is the next step : declare all classes, all variables, all methods ?

Guy Decoux

if what you mean by defined is that storage is reserved for a symbol, as
oposed to declaring a symbol, then isn’t the above exactly what you suggest?

eg.

@sum = @smu + 1

can be tracked down precisely because @smu has not been defined?

my interpretation of ‘define’ is something along the lines of

appears as an lvalue
appears in a ‘def’ statement
appears as symbol in attr :symbol
etc.

in fact, i don’t think there is a way to declare, without defining, a symbol
in ruby? does anyone know otherwise?

that being the case

while trying to define @smu using

@sum = @smu + 1

the error can be tracked down because @smu has not been defined.

personally i would not like to see mandatory declarations as part of ruby,
though optional ones might at times be usefull :

eg.

a wanna-be declaration

def foo; end

puts foo

def foo
‘foo’
end

i’m no language designer by a long shot, but i think this might slow the
interpreter down if this worked by default? if not, i think it would be a
nice feature… right now the only work around (to maintain readability) is
to put foo into a module and do

require ‘foo’

puts foo

which is o.k. most of the time, but sometimes a real optional ‘declaration’ would be
nice too

perhaps a new block method like

declare {
class Forward
def method oneArg, twoArgs, threeArgs, four
end
end
}

could force two passes of the interpreter?

i’m guessing matz has good reason for having be a one pass interpreter though…

-a

···

On Fri, 18 Oct 2002, Kontra, Gergely wrote:

The current behavior is a useful way to catch programming errors such
as

@sum = @smu + 1

This can be tracked down if all fields must be explicitly defined.

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

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
====================================

However he also stated that it worked in Perl which to me shows that he
has not thinking in terms of object / method / reciever but in terms of
type coercion of a non OO language.

No ! Be sure that “he has thinking in terms of object / method / reciever”
;-).
I say that because i’m transleting the receipes of “Perl Cookbook” in Ruby
(pleac.sf.net) and so i have to deal with Perl code.

I was thinking of something like
class NilClass
def +(other)
other if (other.type == Array) or …
raise “method + of nil not defined for this object”
end
end

and

nil = nil + 1 won’t cause a problem is u don’t define the method ‘=’ for
nil.
But I’m sure that, if Dave says that it is a bad thing, he’s right ;-).

> @sum = @smu + 1

This can be tracked down if all fields must be explicitly defined.

if what you mean by defined is that storage is reserved for a symbol, as
oposed to declaring a symbol, then isn't the above _exactly_ what you suggest?

eg.

@sum = @smu + 1

_can_ be tracked down precisely _because_ @smu has not been defined?

What if smu is the correct spelling?

+-[Kontra, Gergely @ Budapest University of Technology and Economics]-+

        Email: kgergely@mcl.hu, kgergely@turul.eet.bme.hu |
URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |

+-------"Olyan langesz vagyok, hogy poroltoval kellene jarnom!"-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

ahoward wrote:

personally i would not like to see mandatory declarations as part of ruby,
though optional ones might at times be usefull :

eg.

a wanna-be declaration

def foo; end

puts foo

def foo
‘foo’
end

i’m no language designer by a long shot, but i think this might slow the
interpreter down if this worked by default? if not, i think it would be a
nice feature… right now the only work around (to maintain readability) is
to put foo into a module and do

require ‘foo’

puts foo

which is o.k. most of the time, but sometimes a real optional ‘declaration’ would be
nice too

What I do:

···

======================================================
#some init
#logic is here
a=foo()
#print output

BEGIN {
def foo
#whatever
end
}

That way I have the logic in the top of my script and the nitty-gritty in the bottom.


Brigands will demand your money or your life, but a woman will demand both.
Make Software - Not War.

duh! can’t believe i didn’t think of that… i do it perl all the time too!
:wink:

good idea.

-a

···

On Mon, 21 Oct 2002, Anders Rosendal wrote:

BEGIN {
def foo
#whatever
end
}

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

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
====================================