A problem outputting text

Hi,

Somewhere in the code you have written
puts asong.name
puts asong.duration

When I run these lines basically it gives me some address in the memory
location ( or atleast that's wot I understand)
How does it help using puts in this manner? I am sure there is a reason
behind doing this? What is the advantage?

Thanks,
Vidhi.

···

-----Original Message-----
From: Steve Callaway [mailto:sjc2000_uk@yahoo.com]
Sent: Friday, February 11, 2005 12:16 PM
To: ruby-talk ML
Subject: Re: a problem outputting text

Try this:

class Song
attr_reader :name, :duration
attr_writer :name, :duration

def initialize ( name, duration)
         @name = name
         @duration = duration
end

def print
    puts "The song title is #{@name} and the track
length is #{@duration} "
end

def set_duration( newduration)
    @duration = newduration
end

end

asong = Song.new( "Good Golly Miss Molly", 240)
asong.print
puts asong.name
puts asong.duration

The problem is the embracement in print:
             below

      puts {"The song title is #{@name} and the
      track length is
      #{@duration} /n" } <- and here
                            
Also be wary of using variables with the same name as
methods as in e.g. duration since Ruby will try and
evaluate the variable as a method or vice versa
dependant on where you are in the code.

--- "Ghelani, Vidhi" <vidhi.ghelani@intel.com> wrote:

Hey,

I am trying to test some of my code. Here it is :

************************************************************************

*************************

class Song

     def duration= (newDuration)

         @duration = newDuration

     end

     def initialize(name, duration)

         @name = name

         @duration = duration

     end

     attr_reader :name, :duration

     attr_writer :name, :duration

    def print

      puts {"The song title is #{@name} and the
track length is
#{@duration} /n" }

    end

  end

    asong = Song.new("Mahi Ve", 2443)

    asong.print

    asong.name

    asong.duration

************************************************************************

***********************

However, this does not print anything. There are no
errors showing up
either. All that happens is that it prints blank
lines.

Does anyone know where I am going wrong ?

Any help would be appreciated.

Thanks,

Vidhi