A problem outputting text

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

try this...

#---- test.rb ----

class Song
   attr_writer :name, :duration

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

   def print
     puts "The song name is #{@name} and the duration is {@duration}\n\n"
   end
end

class Song2
   attr_writer :name, :duration

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

   def to_s
     return "The song name is #{@name} and the duration is duration}\n\n"
   end
end

if $0 == __FILE__

   # create an instance of Song
   asong = Song.new("Some Bangin' Tune", 2443)
   puts asong
   asong.print

   # change attribute values
   asong.name = "A New Tune"
   asong.duration = 3750
   puts asong
   asong.print

   # create an instance of Song2
   asong = Song2.new("Another Bangin' Tune", 2660)
   puts asong

end

#---- end test.rb ----

a few things to note... in your original version you had:

puts { "text here" }

Ruby sees the braces as a block associated with the call to puts, rather than an argument to puts, which is why it was printing blank lines

I also think attr_writer implies attr_reader so you don't need both when using attr_writer.

Also, by adding a method to_s to a class (as in Song2), it is automatically called when you "puts" the varas opposed to just printing out the address

Hope this helps...

Cheers,
Tim

Ghelani, Vidhi 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

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

Hi --

···

On Sat, 12 Feb 2005, Tim Ferrell wrote:

I also think attr_writer implies attr_reader so you don't need both when using attr_writer.

It doesn't; they're separate. You can use #attr_accessor to do both
at once.

David

--
David A. Black
dblack@wobblini.net

Hi --

Try this:

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

You can also do:

   attr_accessor :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

There's no point having that, if you've already got a writeable
'duration' attribute :slight_smile: Actually one of the nice things about the
attr_*-style technique is that you don't have to have methods with
'set' and 'get' in their names. Instead, you just do:

   class Song
     attr_accessor :duration
   #...
   end

   s = Song.new
   s.duration = 180

David

···

On Sat, 12 Feb 2005, Steve Callaway wrote:

--
David A. Black
dblack@wobblini.net

Thanks for the clarification ... I am new here too :slight_smile:

Cheers,
Tim

David A. Black wrote:

···

Hi --

On Sat, 12 Feb 2005, Tim Ferrell wrote:

I also think attr_writer implies attr_reader so you don't need both when using attr_writer.

It doesn't; they're separate. You can use #attr_accessor to do both
at once.

David

Ah, never knew that. I have coded with a get and set
mechanism since time immemorial and never really used
the accessors before. Thanks for helping me throw off
those shackles, David. :slight_smile:

···

--- "David A. Black" <dblack@wobblini.net> wrote:

Hi --

On Sat, 12 Feb 2005, Steve Callaway wrote:

> Try this:
>
> class Song
> attr_reader :name, :duration
> attr_writer :name, :duration

You can also do:

   attr_accessor :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

There's no point having that, if you've already got
a writeable
'duration' attribute :slight_smile: Actually one of the nice
things about the
attr_*-style technique is that you don't have to
have methods with
'set' and 'get' in their names. Instead, you just
do:

   class Song
     attr_accessor :duration
   #...
   end

   s = Song.new
   s.duration = 180

David

--
David A. Black
dblack@wobblini.net