Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

James Nelson
James Nelson
23,956 Points

Ruby to_s

Hello guys,

I am practising what I have learnt Ruby foundations, not quite finished the course yet. I am writing a small movies program which takes in multiple numbers of movies, the user can then sort the movie on 'date', 'rating' etc.

I have it working however I can't quite get the to_s function to work correctly. I am not sure quite sure where it the to_s function would be placed as I have used a module as well as a class.

  ```class Movie
include Enumerable 
def initialize(movie, year)
    @year = []
    @year.push(year)
    @title = movie
    @rating = []
    while getrating
        puts "Rating added"
    end
end

def getrating
    puts "Plese rate #{@title} this movie from 1-5. Enter a number above or below 5 to finish"
    getrating = gets.chomp
    getratingint = getrating.to_i
    if getratingint > 0 && getratingint <= 5
        @rating.push(getratingint)
    else
        puts "<------------------------------------------------>"
        puts "           #{@title} - Rating ended               "
        puts "<------------------------------------------------>"
        puts""
        return nil
    end
end

def avg
    @avg = @rating.inject(0){|sum,iterator| sum + iterator} / @rating.size
end

def year
    @year
end 
    end

    module Sort
def sorton(array, average = true)
    if average == true
        array.sort{|a,b| a.avg <=> b.avg}
    else
        snorted = array.sort{|a,b| a.year <=> b.year}
    end
end 
    end

  movie1 = Movie.new("Inception", "2012")
  movie2 = Movie.new("Memento", "2003")
  movie3 = Movie.new("Movie three", "2001")
  movies = [movie1, movie2, movie3]
  include Sort
  puts "#{sorton(movies, true)}" ```

Looking forward to replies :)

2 Answers

I tried to explain below

class Movie
    include Enumerable

    def initialize(movie, year)
        @year = [] 
        @year.push(year)
        @title = movie
        @rating = []

        while getrating
            puts "Rating added" end end

    def getrating
        puts "Plese rate #{@title} this movie from 1-5. Enter a number above or below 5 to finish"
    getrating = gets.chomp
    getratingint = getrating.to_i

        if getratingint > 0 && getratingint <= 5
            @rating.push(getratingint)
        else
            puts "<------------------------------------------------>"
            puts "           #{@title} - Rating ended               "
            puts "<------------------------------------------------>"
            puts""
            return nil
        end
    end

    def avg
        @avg = @rating.inject(0){|sum,iterator| sum + iterator} / @rating.size
    end

    def year
        @year
    end 

  # to_s method must be declared in the class  
  def to_s
      "#{@title} - #{@year} : #{@rating}"
  end
end

module Sort
  def sorton(array, average = true) 
      if average == true 
          array.sort{|a,b| a.avg <=> b.avg} 
      else 
          array.sort{|a,b| a.year <=> b.year} 
      end
    end
end

movie1 = Movie.new("Inception", "2012")
movie2 = Movie.new("Memento", "2003")
movie3 = Movie.new("Movie three", "2001")
movies = [movie1, movie2, movie3] 

include Sort 

# Your code prints the array, so print each of items separately
sorton(movies, true).each { |movie| puts movie }