What is wrong in this code ...?

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

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

···

On 7/5/05, Faisal Raja <Raja.Faisal@gmail.com> wrote:

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.jaredrichardson.net

"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

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

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

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.

···

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

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

···

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 .

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

May U all be blessed

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

May U all be blessed

Well, if we're doing stylistic suggestions... :slight_smile:

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