Jim's
Tutorials

Fall 2018
course
site

Rails Week 5

Back to Hashes

So I kind of rushed things to prepare for the interview and skipped right to objects before becoming comfortable with symbols and hashes. I decided to backtrack a little and do problems from this section of the curriculum. Below I start by iterating through a list of golf clubs and printing a random number of them as a command, and the rest as a warning that they are not included in the bag.

I messed up initially as I tried to set the condition that those not in the bag would be printed on the screen, and ended up printing everything on the screen if a single item was not included in the bag:

REQUIRED_BAG_SIZE = 10

available_clubs = [
  :two_iron,
  :three_iron,
  :four_iron,
  :five_iron,
  :six_iron,
  :seven_iron,
  :eight_iron,
  :nine_iron,
  :pitching_wedge,
  :sand_wedge,
  :driver,
  :three_wood,
  :five_wood,
  :seven_wood,
  :putter
]

possible_club_selections = available_clubs.sample(REQUIRED_BAG_SIZE)
possible_club_selections.each do |n|

if possible_club_selections.all?{|n|}
    puts "use the #{n}!"
else
 puts "Hi !#{n}"
end
end

OUTPUT:

Hi !three_wood
Hi !three_iron
Hi !eight_iron
Hi !pitching_wedge
Hi !sand_wedge
Hi !two_iron
Hi !five_iron
Hi !six_iron
Hi !five_wood
Hi !putter

So conditionals within iterations were giving me some trouble. Then I realized I had to make another variable and loop through it and came out a bit better:

possible_club_selections = available_clubs.sample(REQUIRED_BAG_SIZE)
possible_club_selections.each do |n|
puts "use the #{n}!"

end

the_rest = available_clubs - possible_club_selections
the_rest.each do |n|
    puts "warning! you don't have #{n}"
end

OUTPUT:

use the pitching_wedge!
use the driver!
use the four_iron!
use the nine_iron!
use the seven_iron!
use the two_iron!
use the six_iron!
use the sand_wedge!
use the putter!
use the three_iron!
warning! you don't have five_iron
warning! you don't have eight_iron
warning! you don't have three_wood
warning! you don't have five_wood
warning! you don't have seven_wood

Finally I realized that I'd followed the instructions wrong and they actually only wanted to know if my random selection DID NOT INCLUDE either a driver, a pitching wedge, or a putter. So I tried to do that, and I made something that announces if I don't have a putter... to begin with...

possible_club_selections = available_clubs.sample(REQUIRED_BAG_SIZE)
possible_club_selections.each do |n|
puts "use the #{n}!"
end
the_rest = :putter
if possible_club_selections.include?(the_rest) == false
puts "#{the_rest} is not included in your bag!"
end

OUTPUT:

use the seven_wood!
use the sand_wedge!
use the six_iron!
use the pitching_wedge!
use the five_wood!
use the nine_iron!
use the five_iron!
use the four_iron!
use the two_iron!
use the three_iron!
putter is not included in your bag!

But then I thought I'd designated the_rest to :putter incorrectly, forcing the answer, so I started over in this super frustrating process that eventually lead me to the correct answer (see below:

require 'pry'
REQUIRED_BAG_SIZE = 10

available_clubs = [
  :two_iron,
  :three_iron,
  :four_iron,
  :five_iron,
  :six_iron,
  :seven_iron,
  :eight_iron,
  :nine_iron,
  :pitching_wedge,
  :sand_wedge,
  :driver,
  :three_wood,
  :five_wood,
  :seven_wood,
  :putter
]

possible_club_selections = available_clubs.sample(REQUIRED_BAG_SIZE)
possible_club_selections.each do |n|
puts "use the #{n}!"

end


stuff_looking_for = [:putter,:driver,:sand_wedge]
stuff_looking_for.each do |n|
if possible_club_selections.include?(n) == false
puts "#{n} is not included in your bag!"

end
end

OUTPUT:

use the eight_iron!
use the five_wood!
use the three_wood!
use the six_iron!
use the seven_wood!
use the three_iron!
use the five_iron!
use the sand_wedge!
use the driver!
use the two_iron!
putter is not included in your bag!

More practice with hashes

Did some stuff to access stuff in hashes, like this:

golf_contacts = {
  jim: 'workerBee@example.com',
  samantha: 'golfCartRacer@example.com',
  jane: 'pro_golfer89@example.com',
  mike: 'alwaysAtTheBeach@example.com',
  olivia: 'didYouSeeWhereThatWent@example.com',
  joan: 'bestShortGameEver@example.com'
}


    puts [golf_contacts[:samantha], golf_contacts[:jane], golf_contacts[:olivia]].join(',')
    invites = [:samantha, :jane, :olivia]

    invites.each do |email|
        print "#{golf_contacts[email]}"
        if email != invites.last
            print ","
        end

    end

OUTPUT invites everyone in an email list:

ruby golfing.rb
golfCartRacer@example.com,pro_golfer89@example.com,didYouSeeWhereThatWent@example.com
golfCartRacer@example.com,pro_golfer89@example.com,didYouSeeWhereThatWent@example.com%

BUT then i did this, and its like, i got the same answer with no code at all:

golf_contacts = {
  jim: 'workerBee@example.com',
  samantha: 'golfCartRacer@example.com',
  jane: 'pro_golfer89@example.com',
  mike: 'alwaysAtTheBeach@example.com',
  olivia: 'didYouSeeWhereThatWent@example.com',
  joan: 'bestShortGameEver@example.com'
}
puts "to#{golf_contacts.values}"

OUTPUT:

to["workerBee@example.com", "golfCartRacer@example.com", "pro_golfer89@example.com", "alwaysAtTheBeach@example.com", "didYouSeeWhereThatWent@example.com", "bestShortGameEver@example.com"]

gsub

yeah, tried to organize a list using gsub and read this documentation about it... also read this stack overflow convo about gsub and kind of didin't help... here's an example of some output:

driver*200
four*wood*180
five*wood*170
two*iron*170
three*iron*160
four*iron*150
five*iron*140
six*iron*130
seven*iron*120
eight*iron*110
nine*iron*95
pitching*wedge*80
sand*Wedge*20
putter*0
➜  the-right-clubs git:(master) ✗ ruby club.rb
driver200
fourwood180
fivewood170
twoiron170
threeiron160
fouriron150
fiveiron140
sixiron130
seveniron120
eightiron110
nineiron95
pitchingwedge80
sandWedge20
putter0

basically trying to replace stuff so i can access the key value pairs in a given list...