Ruby Class Help

Hello. I'm Ruby beginner, I'm not getting proper output, of course it's
problem in my code not in Ruby itself.
Anyone can help to fix this?

The out put should be:
The Dark Knight Rises | Release Date: 2013 | Rating: 7.
But I'm also getting this "#<Movie:0x102b26620>" thing. Anyone can help
me why this is coming even after I used "to_s" ? your help will be
appreciated.

···

===============

class Movie
  def initialize(title, date, rating)
  @title = title
  @date = date
  @rating = rating
  end

  def to_s
    puts "Name: #{@title} | Release Date: #{@date} | Rating:
#{@rating}."
  end

  def thumbs_up
    @rating += @rating
  end

  def thumbs_down
    @rating -= @rating
  end

end

TheDarkKnight = Movie.new("The Dark Knight Rises", 2013, 7)

puts TheDarkKnight

--
Posted via http://www.ruby-forum.com/.

Quoting Rocky D. (lists@ruby-forum.com):

Hello. I'm Ruby beginner, I'm not getting proper output, of course it's
problem in my code not in Ruby itself.
Anyone can help to fix this?

to_s must *return* the string, not print it out.

Just remove the 'puts'.

Carlo

···

Subject: Ruby Class Help
  Date: gio 12 set 13 07:32:25 +0200

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Carlo E. Prelz wrote in post #1121238:

Subject: Ruby Class Help
  Date: gio 12 set 13 07:32:25 +0200

to_s must *return* the string, not print it out.

Just remove the 'puts'.

Carlo

Wow! Fixed! Thank you, Carlo. and I want to increase the rating with
this, at the end:

···

==========
TheDarkKnight = Movie.new("The Dark Knight Rises", 2013, 7)

3.times do
  TheDarkKnight.thumbs_up
  end

puts TheDarkKnight

===========

But it shows 56 rating instead of 10! What am I doing wrong? Please tell
me.

--
Posted via http://www.ruby-forum.com/\.

Carlo E. Prelz wrote in post #1121238:

Subject: Ruby Class Help
  Date: gio 12 set 13 07:32:25 +0200

Quoting Rocky D. (lists@ruby-forum.com):

Hello. I'm Ruby beginner, I'm not getting proper output, of course it's
problem in my code not in Ruby itself.
Anyone can help to fix this?

to_s must *return* the string, not print it out.

Just remove the 'puts'.

Carlo

Yes, removed 'puts' and problem solved! Another thing I was doing wrong
was using "@rating += @rating" I've no idea why did I wrote that! it
should be "@rating += 1" :slight_smile: same for thumbs down method.
Thank you Carlo for help :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.