[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Faisal Raja

7/5/2005 7:14:00 PM

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

9 Answers

threeve.org

7/5/2005 7:24:00 PM

0

On 7/5/05, Faisal Raja <Raja.Faisal@gmail.com> wrote:
> 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 .
>

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
>
>
> 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 = "Name: " + @name

> I wuld be thankful if u people help me out
> Thanks in advance
>
>
>


Jason


Jared Richardson

7/5/2005 7:30:00 PM

0

I'm no Ruby guru (and I'm sure you'll hear from others soon), but if you add

def printName
puts @name
end


then use this

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

you can run it like so:

C:\dev\ruby>student.rb testName
Abc

C:\dev\ruby>

Jared

Ship It! is shipping!
http://www.jaredrich...
http://www.pragmaticprogrammer.com/t...


"Faisal Raja" <Raja.Faisal@gmail.com> wrote in message
news:1120590863.719406.196200@o13g2000cwo.googlegroups.com...
> 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
>


Daniel Nugent

7/5/2005 7:37:00 PM

0

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.

On 7/5/05, Jason Foreman <threeve.org@gmail.com> wrote:
> On 7/5/05, Faisal Raja <Raja.Faisal@gmail.com> wrote:
> > 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 .
> >
>
> 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
> >
> >
> > 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 = "Name: " + @name
>
> > I wuld be thankful if u people help me out
> > Thanks in advance
> >
> >
> >
>
>
> Jason
>
>


--
-Dan Nugent


Gavin Kistner

7/5/2005 7:44:00 PM

0

On Jul 5, 2005, at 1:37 PM, Daniel Nugent wrote:
> 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.

Faisal Raja

7/5/2005 7:48:00 PM

0

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

Belorion

7/5/2005 7:53:00 PM

0

On 7/5/05, Faisal Raja <Raja.Faisal@gmail.com> wrote:
> 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 STDOUT.

Matt


Faisal Raja

7/5/2005 7:57:00 PM

0

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

May U all be blessed

Faisal Raja

7/5/2005 7:57:00 PM

0

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

May U all be blessed

mathew

7/8/2005 5:46:00 PM

0

Well, if we're doing stylistic suggestions... :-)

class Student
attr_accessor :name

def initialize(name)
@name = name
end

def to_s
return "Name: #{@name}"
end
end

attr_accessor is an easy way to set up a read/write instance variable;
it creates an accessor method so you can go object.name = "John Smith"
or puts object.name

There's also attr_reader to set up a read-only field, and attr_writer
for write-only.

The to_s method is the standard method used to convert an object to a
string; so if you define it to be something sensible, you can just print
or puts the object directly.

So, we can now do

s = Student.new("John Smith")
puts s
s.name = "John P. Smith"
puts s.name


mathew