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

Running a function on an array item

Hello, I am messing around with a program to learn a little more about arrays.

What I plan the program to do is firstly, get a collection of movies and store them in an array. For each movie in an array I would like to assign with a release year. I have the following which does not seem to work.

   '''class Movies

   def initialize 
   @movies = []
       while gettitle
           puts "Movie added"
       end
   end

   def gettitle
       puts "What is the title of the movie? Pass in a space to finish"
       movie = gets.chomp
       if movie.length > 0
           @movies.push(movie)
       else
           return nil
       end
   end

   def to_s
       puts "#{@movies}"
   end

   def getyear
       puts "What year"
  end

    end

   moviecollection1 = Movies.new #returns array
   moviecollection1.each {|year| year.getyear()}'''

I am not quite sure what I'm doing wrong here. If someone could help me with the programs logic that would be fantastic.

Thanks in advance

2 Answers

David Smith
David Smith
21,220 Points

Below is my code on how i might approach this problem (BTW not the only way this could be done) I have added in comments to the code the understand the added parts

Hope this helps

class Movies

   def initialize 
      @movies = []
       while gettitle
           puts "Movie added"
       end
   end

   def gettitle
            details = [];
        # changed your program a litle bit to get the name of the movie and year at the same time
                puts "What is the title of the movie? Pass in a space to finish"
                film = gets.chomp
                puts "what is the year of the movie"
                year = gets.chomp
                if film.length > 0
            # store the date film and year into an array, 
            # then store that array into the @movies array
                details[0] = film
                 details[1] = year
            @movies.push(details)
       else
           return nil
       end
   end
   # this method will return just the movie array
   def return_movie_list
    @movies
   end

   def to_s
       puts "#{@movies}"
   end
  # this method will print out a movie details from the movie that was passed in
  def getyear(movie)
       puts "the movie #{movie[0]} was made in #{movie[1]}"
  end

end

moviecollection1 = Movies.new #returns array
# the line below will return an array from calling the method return_movie_list
# then using each on the array print out the movie and year to the screen via the getyear method
moviecollection1.return_movie_list.each {|movie| moviecollection1.getyear(movie)}
James Nelson
James Nelson
23,956 Points

Hey David, thanks for this. Not quite what I was looking for but great solution none the less. Thanks