Jim's
Tutorials

Fall 2018
course
site

Rails week 7

Back to making objects..

So I tried to make something and it didin't work as I'd hoped.

#Leslie Wilson
#Rails Homework Week 7


#This program defines a theater constructor so that it requires a number of seats. I'm trying to also maintain the number of patrons admitted with each instance. The number of patrons should start out as zero.

#Then I'm defineing an admit! instance method that optionally takes a single argument. This argument should represent the number of people to admit. It should default to 1 if the argument is not specified.Each time it is called, it should increase the number of patrons in the theater.

#If the number of patrons to admit exceeds capacity, I don't want to admit them. I want to  provide the usher with an error message and leave the number of admitted patrons unchanged. I want to write an instance method at_capacity? that checks to see if the Theater is at capacity. It should return true if the total number of patrons is equal to the number of seats. If the number of patrons hasn't reached capacity, the method should return false.


patrons = 0
class Theater
    attr_accessor :seats, :patrons
    def initialize (seats, patrons)
        @seats = seats
        @patrons = patrons

    end

    def at_capacity
        if @patrins >= @seats
            return true
        else
            return false
        end

    end

    def admit(number = 1)
        if at_capacity == true
            puts "sorry you can't come in"
        else
            @patrons = @patrons  += 1
            puts number
        end

    end

end

...The resulting output gives me nothing. I'm not running my instance methods correctly somehow, and tried running Theater.new with two arguments and don't really get anything. So I looked up some solutions for this and here are the fixes, and then I'll talk about what I did wrong.

SOLUTION:

..So a few minor things. First, I needed to put the counter within the method. Second, I can pass one argument through the initialize that has an entirely different name, which I did not realize.

class Theater
    attr_accessor :seats, :patrons
    def initialize (number_of_seats)
        @seats = number_of_seats
        @patrons = 0

    end

And...this is my final result that worked:

class Theater
    attr_accessor :seats, :patrons
    def initialize(number_seats)
        @seats = number_seats
        @patrons = 0

        def admit(number = 1)
                @patrons = @patrons += number
        end

    def at_capacity
        if @patrons > @seats
            puts "nope"
        else
            return false
        end

    end


end
end

theater = Theater.new(2)
theater.admit(4)
theater.at_capacity

OUTPUT:

ruby soccer.rb
would you like to add some players? (say yes or no)
yes
6%                                                                                                                                             ➜  soccer git:(master) ✗ ruby soccer.rb
would you like to add some players? (say yes or no)
no
ok never mind then
5%

A soccer team object

# Leslie Wilson
# Objects Week 7

# make a soccer team object that initiates with an array of players, make a method that adds players, a method that removes players, teams can only have 15 players and have to have at least 11 players so factor that in to your object


class Team
    attr_accessor :players
    def initialize(players)

        array = []
        array << players

        @players = players

    end

    def add_players(number = 1)
        puts "would you like to add some players? (say yes or no)"
        input = gets.chomp
        if input == 'yes'
             @players += number
        else
            puts "ok never mind then"
end

end

end


rollcall = Team.new(5)
rollcall.add_players
print rollcall.players

OUTPUT:

ruby soccer.rb
would you like to add some players? (say yes or no)
yes
6%                                                                                                                                           
would you like to add some players? (say yes or no)
no
ok never mind then
5%

MORE- a jukebox object

# Leslie Wilson

#Your Jukebox constructor should take in a playlist of songs as an argument. This playlist represents the songs that you are requesting the jukebox to play.

#Like any good jukebox, it is limited to a certain set of songs that it can actually play. Below is the list of songs the jukebox has to offer. Your constructor should eliminate any songs from your requested playlist that are not on this list:

# Hello - Lionel Ritchie
# Kokomo - The Beach Boys
# Girl You Know It’s True - Milli Vanilli
# Agadoo - Black Lace
# Down Under - Men at Work
# Nothing's Gonna Stop Us Now - Starship
# Get Outta My Dreams, Get Into My Car - Billy Ocean
# I Just Called To Say I Love You - Stevie Wonder
# Hangin' Tough - New Kids on the Block
# We Built This City - Starship
# Wake Me Up Before You Go Go - Wham!
# We Didn't Start The Fire - Billy Joel
# I Wanna Dance With Somebody - Whitney Houston
# U Can't Touch This - MC Hammer

FIRST I TRY TO SPLIT LINES AND ACCIDENTALLY END UP SPLITTING EVERY WORD USING THE FOLLOWING:


# individual_words = []
# File.open('songs.txt').each do |line|
#     split_lines = line.split(" ")
#     individual_words << split_lines
#
# end
#
# print individual_words

I TRIED ANOTHER WAY, THAT ENDED UP WORKING:


new_list = "Hello - Lionel Ritchie
Kokomo - The Beach Boys
Girl You Know It’s True - Milli Vanilli
Agadoo - Black Lace
Down Under - Men at Work
Nothing's Gonna Stop Us Now - Starship
Get Outta My Dreams, Get Into My Car - Billy Ocean
I Just Called To Say I Love You - Stevie Wonder
Hangin' Tough - New Kids on the Block
We Built This City - Starship
Wake Me Up Before You Go Go - Wham!
We Didn't Start The Fire - Billy Joel
I Wanna Dance With Somebody - Whitney Houston
U Can't Touch This - MC Hammer".lines.map(&:chomp)

print new_list

OUTPUT:


ruby juke.rb
["Hello - Lionel Ritchie", "Kokomo - The Beach Boys", "Girl You Know It’s True - Milli Vanilli", "Agadoo - Black Lace", "Down Under - Men at Work", "Nothing's Gonna Stop Us Now - Starship", "Get Outta My Dreams, Get Into My Car - Billy Ocean", "I Just Called To Say I Love You - Stevie Wonder", "Hangin' Tough - New Kids on the Block", "We Built This City - Starship", "Wake Me Up Before You Go Go - Wham!", "We Didn't Start The Fire - Billy Joel", "I Wanna Dance With Somebody - Whitney Houston", "U Can't Touch This - MC Hammer"]%

I PROCEEDED TO MAKE THE JUKEBOX OBJECT:


class Jukebox
    attr_accessor :eliminate
    def initialize(playist)
        @playlist = playlist
        playlist << new_list
    end
end
    print Jukebox.playlist

THEN I PROCEEDED TO ACTUALLY MAKE THE METHODS THEY WERE ASKING FOR: METHOD 1: Implement a play! instance method that takes the first song on the list and removes it from the playlist permanently. Upon calling this method, the playing song should be returned in your terminal.

def play!
        @playlist.pop
    end

And this one:Implement a shuffle_songs! method that randomizes the order of your playlist.

def shuffle_songs
        @playlist.sort_by {rand}
    end

And this: Implement an add_track instance method that requires one argument, the name of the track. If the song is in list of available songs, your software should add the song the the end of the playlist and return true. If the song is not found on the list, it should just return false.

def add_track(name)
    @name = name
    if @playlist.include?(name)
        @playlist << name
    else
        print "sorry, that song isin't an option"
        end
    end
end

FOR THE ENTIRE THING:

class Jukebox
    attr_accessor :playlist, :blah
    def initialize()
        @playlist = "Hello - Lionel Ritchie
Kokomo - The Beach Boys
Girl You Know It’s True - Milli Vanilli
Agadoo - Black Lace
Down Under - Men at Work
Nothing's Gonna Stop Us Now - Starship
Get Outta My Dreams, Get Into My Car - Billy Ocean
I Just Called To Say I Love You - Stevie Wonder
Hangin' Tough - New Kids on the Block
We Built This City - Starship
Wake Me Up Before You Go Go - Wham!
We Didn't Start The Fire - Billy Joel
I Wanna Dance With Somebody - Whitney Houston
U Can't Touch This - MC Hammer".lines.map(&:chomp)
    end
    def play!
        @playlist.pop
    end
    def shuffle_songs
        @playlist.sort_by {rand}
    end
def add_track(name)
    @name = name
    if @playlist.include?(name)
        @playlist << name
    else
        print "sorry, that song isin't an option"
        end
    end
end

    @playlist = Jukebox.new()
    print @playlist.add_track("Agadoo - Black La")

SOURCES : This resource for splitting lines I found on stackoverflow