What is wrong in this code ... ???

F

Faisal Raja

Hello ,
Well Im new to programming in Ruby Language ( Infact only an hour back
i started it ) .

class Student

def initialize(name)

@name = name

end

def show

s = "Name : " + name
return s


end
end


st = Student.new("Abc") ;
st.inspect

I have Windows Xp Sp2 n when i run this program on console , it doesn't
show me anything . There is no error but it is also not shwoing me
anything .

st.inspect should show info
st.to_s also doesn't show anything


if i write st.show
it also doesn't work .
I wuld be thankful if u people help me out
Thanks in advance
 
J

Jason Foreman

Hello ,
Well Im new to programming in Ruby Language ( Infact only an hour back
i started it ) .
=20
class Student
=20
def initialize(name)
=20
@name =3D name
=20
end
=20
def show
=20
s =3D "Name : " + name
return s
=20
=20
end
end
=20
=20
st =3D Student.new("Abc") ;
st.inspect
=20
I have Windows Xp Sp2 n when i run this program on console , it doesn't
show me anything . There is no error but it is also not shwoing me
anything .
=20

you aren't printing anything. Try putting "p st.inspect" instead of
just "st.inspect".
st.inspect should show info
st.to_s also doesn't show anything
=20
=20
if i write st.show
it also doesn't work .

this is because you reference 'name' which is a local variable, but
what you meant is to access '@name' which is the instance variable.

Try this instead: s =3D "Name: " + @name
I wuld be thankful if u people help me out
Thanks in advance
=20
=20
=20


Jason
 
D

Daniel Nugent

The extra assignment is unnecessary too.

you can just do

def show
return "Name: " + @name
end

With instance and class variables, the @'s are part of the name themselves.

This is a nice thing because you can always tell whether you're
looking at a class variable, a local variable, or an instance
variable.

=20
you aren't printing anything. Try putting "p st.inspect" instead of
just "st.inspect".
=20
=20
this is because you reference 'name' which is a local variable, but
what you meant is to access '@name' which is the instance variable.
=20
Try this instead: s =3D "Name: " + @name
=20
=20
=20
Jason
=20
=20


--=20
-Dan Nugent
 
G

Gavin Kistner

--Apple-Mail-1-551515356
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

The extra assignment is unnecessary too.

you can just do

def show
return "Name: " + @name
end

Or, because the last value in a method is the return value (in the
absence of a call to the #return method) you can simply do:

def show
"Name: " + @name
end

or, even better, use string interpolation:

def show
"Name: #{@name}"
end

and finally, because instance variables are special, even:

def show
"Name: #@name"
end

will work.
--Apple-Mail-1-551515356--
 
F

Faisal Raja

Well , p st.inspect worked ... thanks .
What this 'p' represent ??? .... why we need to include it .
 
B

Belorion

Well , p st.inspect worked ... thanks .
What this 'p' represent ??? .... why we need to include it .

st.inspect just returns "... a string containing a human-readable
representation of obj"

You can do whatever you want with that string -- it does not assume
you want to send it to STDOUT.

p st.inspect is short for print st.inspect, which sends the string to STDOU=
T.

Matt
 
F

Faisal Raja

Well I got it . Thanks you every1 , for helping
U people have given me some useful things .

May U all be blessed
 
F

Faisal Raja

Well I got it . Thanks you every1 , for helping
U people have given me some useful things .

May U all be blessed
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top