[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Something strange in ruby or I'm a newbie?

Hussachai Puripunpinyo

9/20/2006 10:32:00 AM

Question 1:

#IS THIS A BUG
class TestBug
def test
"Hello Bug"
end
end
class FoundBug < TestBug
def test
super +" ,I found you !" #Notice here
end
end
test = FoundBug.new()
puts test.test

It blame me that
"undefined method `+@' for " ,I found you !":String (NoMethodError)
from C:/DOCUME~1/HUSSAC~1/LOCALS~1/Temp/rb1F.tmp:12"
Why?
This is bug or I miss something?

If I replace the line in '#Notice here' to

super+" ,I found you !" #With no space between super and my string
#OR
super + " ,I found you !" #With space both side of '+'
#I found that no error

Why?

--
Posted via http://www.ruby-....

11 Answers

Hussachai Puripunpinyo

9/20/2006 10:37:00 AM

0

Question 2:

nil in ruby can put in condition statement
such as if(nil) it will assume that's false

ex.
if(nil)
else
puts "nil is false"
end

sure it show me the string
Becuase I'm curious,I want to test more

puts nil==nil #Yeah that's true

puts !nil #true

puts !nil==true #true

puts nil==false #FALSE !!!

What happen in the last statement? [or I miss something]




--
Posted via http://www.ruby-....

Ryan Davis

9/20/2006 10:42:00 AM

0


On Sep 20, 2006, at 3:37 AM, Hussachai Puripunpinyo wrote:

> puts nil==nil #Yeah that's true
> puts !nil #true
> puts !nil==true #true
> puts nil==false #FALSE !!!
>
> What happen in the last statement? [or I miss something]

nil isn't false. Both nil and false are objects in ruby. Compare:

% irb
>> [nil, false].map { |o| o.object_id }
=> [4, 0]


Hussachai Puripunpinyo

9/20/2006 11:23:00 AM

0

Ryan Davis wrote:
> On Sep 20, 2006, at 3:37 AM, Hussachai Puripunpinyo wrote:
>
>> puts nil==nil #Yeah that's true
>> puts !nil #true
>> puts !nil==true #true
>> puts nil==false #FALSE !!!
>>
>> What happen in the last statement? [or I miss something]
>
> nil isn't false. Both nil and false are objects in ruby. Compare:
>
> % irb
> >> [nil, false].map { |o| o.object_id }
> => [4, 0]

Thank you Davis.
I assume you told me that "==" mean "compare with object_id that's
right?
Uhmm.. it's OK in other lang use this idea such as Java

negative of every object(I'm not sure) in ruby except nil and false
is false;
negative of nil and false is true

How about 1 and 1.0 both are the same object?
the rule that I just assume above was broken?

and why every object except nil and false return false
what's the reason? there have already exist in the FAQ?



--
Posted via http://www.ruby-....

Morton Goldberg

9/20/2006 11:34:00 AM

0

On Sep 20, 2006, at 6:32 AM, Hussachai Puripunpinyo wrote:

> Question 1:
>
> #IS THIS A BUG
> class TestBug
> def test
> "Hello Bug"
> end
> end
> class FoundBug < TestBug
> def test
> super +" ,I found you !" #Notice here
> end
> end
> test = FoundBug.new()
> puts test.test
>
> It blame me that
> "undefined method `+@' for " ,I found you !":String (NoMethodError)
> from C:/DOCUME~1/HUSSAC~1/LOCALS~1/Temp/rb1F.tmp:12"
> Why?
> This is bug or I miss something?
>
> If I replace the line in '#Notice here' to
>
> super+" ,I found you !" #With no space between super and my string
> #OR
> super + " ,I found you !" #With space both side of '+'
> #I found that no error
>
> Why?

No, I'm afraid it's a feature ;). if you rewrite

> super +" ,I found you !" #Notice here

to
> super + " ,I found you !" #Notice space after '+'.

it will work. By leaving out the space, you have invoked the unary
'+' operator. But, of course, you wanted the binary '+' operator. In
Ruby there are times when spaces and parentheses are critical.

Regards, Morton



Hussachai Puripunpinyo

9/20/2006 11:36:00 AM

0


Thank you Dober.
That's make sense.

--
Posted via http://www.ruby-....

Jano Svitok

9/20/2006 11:44:00 AM

0

On 9/20/06, Morton Goldberg <m_goldberg@ameritech.net> wrote:
> On Sep 20, 2006, at 6:32 AM, Hussachai Puripunpinyo wrote:
>
> > Question 1:
> >
> > #IS THIS A BUG
> > class TestBug
> > def test
> > "Hello Bug"
> > end
> > end
> > class FoundBug < TestBug
> > def test
> > super +" ,I found you !" #Notice here
> > end
> > end
> > test = FoundBug.new()
> > puts test.test
> >
> > It blame me that
> > "undefined method `+@' for " ,I found you !":String (NoMethodError)
> > from C:/DOCUME~1/HUSSAC~1/LOCALS~1/Temp/rb1F.tmp:12"
> > Why?
> > This is bug or I miss something?
> >
> > If I replace the line in '#Notice here' to
> >
> > super+" ,I found you !" #With no space between super and my string
> > #OR
> > super + " ,I found you !" #With space both side of '+'
> > #I found that no error
> >
> > Why?
>
> No, I'm afraid it's a feature ;). if you rewrite
>
> > super +" ,I found you !" #Notice here
>
> to
> > super + " ,I found you !" #Notice space after '+'.
>
> it will work. By leaving out the space, you have invoked the unary
> '+' operator. But, of course, you wanted the binary '+' operator. In
> Ruby there are times when spaces and parentheses are critical.

In other words,

super +"whatever" is the same as super(+"whatever") while
super+"whatever" or super + "whatever" is the same as super(*args) + "whatever".

right?

Kroeger, Simon (ext)

9/20/2006 12:05:00 PM

0



> [...]
>
> In other words,
>
> super +"whatever" is the same as super(+"whatever") while
> super+"whatever" or super + "whatever" is the same as
> super(*args) + "whatever".
>
> right?

Yep:
--------------------------
def test *args; 41; end

puts test+1 #=> 42
puts test + 1 #=> 42
#but
puts test 1 #=> 41
#and
puts test +1 #=> 41
--------------------------

cheers

Simon

Morton Goldberg

9/20/2006 12:08:00 PM

0

On Sep 20, 2006, at 7:43 AM, Jan Svitok wrote:

>> it will work. By leaving out the space, you have invoked the unary
>> '+' operator. But, of course, you wanted the binary '+' operator. In
>> Ruby there are times when spaces and parentheses are critical.
>
> In other words,
>
> super +"whatever" is the same as super(+"whatever") while
> super+"whatever" or super + "whatever" is the same as super(*args)
> + "whatever".
>
> right?

That's the way I see it. In Ruby I think its best to consider 'super'
as a pseudo-method call and not as a pseudo-variable as it is most
other OO languages.

Regards, Morton



Morton Goldberg

9/20/2006 12:11:00 PM

0

On Sep 20, 2006, at 7:43 AM, Robert Dober wrote:

>> it will work. By leaving out the space, you have invoked the unary
>> '+' operator. But, of course, you wanted the binary '+' operator. In
>> Ruby there are times when spaces and parentheses are critical.
>>
>> Regards, Morton
>
>
> No he found something really strange
> look at this
>
> puts "a" +"b"
>
> Do you get his point?

No. The two cases aren't comparable. "a" is an object; 'super' is a
pseudo-method.

Regards, Morton



Devin Mullins

9/20/2006 12:41:00 PM

0

> super +" ,I found you !" #Notice here
<snip>
> "undefined method `+@' for " ,I found you !":String (NoMethodError)

Congratulations! You stumped the parser! (Actually, it's really easy.)

Your code is attempting to call super, passing one argument:
+" ,I found you !"
That + is the unary + operator, which exists for Integers (and does
nothing), but doesn't have a definition for strings -- hence the error.

In fact, I'm not really sure why it exists. Might make for some
interesting DSLage:
ThingPrinter.run do
+verbose
-coloring
...
end

Devin