Something strange in ruby or I'm a newbie?

  • Thread starter Hussachai Puripunpinyo
  • Start date
H

Hussachai Puripunpinyo

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?
 
H

Hussachai Puripunpinyo

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]
 
H

Hussachai Puripunpinyo

Ryan said:
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?
 
M

Morton Goldberg

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
 
J

Jan Svitok

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


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?
 
K

Kroeger, Simon (ext)

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

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

puts test+1 #=3D> 42
puts test + 1 #=3D> 42
#but
puts test 1 #=3D> 41
#and=20
puts test +1 #=3D> 41
 
M

Morton Goldberg

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
 
M

Morton Goldberg

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
 
D

Devin Mullins

super +" ,I found you !" #Notice here
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,212
Messages
2,571,102
Members
47,698
Latest member
TerraT521

Latest Threads

Top