Jim's
Tutorials

Fall 2018
course
site

Jim says

We did another version of this in my office ... which is attached.


Rails Week 6

This week tried to integrate some of my ruby stuff more with my testing learning goals. First I tried to create an app with a baseline minimum level of complexity that would allow for some decent tests to be written. I was given a group of numbers and words and needed to turn them into a hash. I experimented again with readline and getting the dataset usable without having to insert hash-rockets and commas manually.

File.open('golfclubs.txt').each do |line|
    split_lines = line.split(" ")
    print split_lines[0]
split_lines.each do |l|



 end
end

...and the txt file included:


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

OUTPUT:

driverfourfivetwothreefourfivesixseveneightninepitchingsandputter%

..So , experimenting with dataset manipulation and extracting information from hashes.

JIM LOOK (I'm so proud)

attempt after attempt lead to FINALLY being able to process the above weird list of golf clubs into hash format without having to do it manually!! check out this code its pure genius if i do say so myself:

container = []
File.open('golfclubs.txt').each do |line|
    split_lines = line.split(" ")
    container << split_lines

end

hash = {}
container.each do |i|
    if i.length == 2

    hash[i[0]] = [i[1]]
else
    hash[i[0]+ "_"+i[1]] = [i[2]]

end
end
print hash

OUTPUT:

the-right-clubs git:(master) ✗ ruby club.rb
{"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) ✗

Here is the whole thing!! it took me so long. lol! getting the key and hash to be connected (and by that I mean, finding the key and displaying it with the value I found) was really hard. Also I read gsub docs for a long time and again didin't work, I ended up doing it the hacky way and just g-subbing twice to get rid of both brackets around the thing that announces your distance (see below)

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


container = []
File.open('golfclubs.txt').each do |line|
    split_lines = line.split(" ")
    container << split_lines

end

hash = {}
container.each do |i|
    if i.length == 2

    hash[i[0]] = [i[1].to_i]
else
    hash[i[0]+ "_"+i[1]] = [i[2].to_i]

end
end

puts "how far away are you"
input = gets.chomp.to_i

newArray = []

hash.values.each do |n|
    if input >= n[0]
        newArray << [n[0]]

    end

end
puts "youre #{newArray[0].to_s.gsub("[", "").gsub("]", "")} feet away! you should use the #{hash.key(newArray[0])}!"

OUTPUT:

how far away are you
20
youre 20 feet away! you should use the sand_Wedge!

What I learned from this

...processing data is really hard and you're constantly having to delve deeper into layers and drag information out of places and attach it to other information.

Iterators

spent some time going over the enumerator documentation to learn some different techniques for accessing information in my hash. used some of the more interesting methods like collect, chunk, etc- all in the spirit of saving myself some time after being handed a big chunk of unorganized data:

chunk = [4,6,2,3,1,5,6].chunk {|n| n.even?}.each{|even, ary| p [even,ary]
}
puts chunk


map = (1..4).map {|i| i*i}
puts map


collection = (1..4).collect { "leslie"}
puts collection


cycle = ['leslie', 'paul', 'maria']
cycle.cycle(2) {|a| puts a }
puts cycle


each_slice = (1..10).each_slice(2) {|a| p a}
puts each_slice


each_with_index = Hash.new
%w(leslie paul maria).each_with_index {|item, index|
each_with_index[item] = index
}
puts each_with_index

OUTPUT

[true, [4, 6, 2]]
[false, [3, 1, 5]]
[true, [6]]

1
4
9
16
leslie
leslie
leslie
leslie
leslie
paul
maria
leslie
paul
maria
leslie
paul
maria
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]

{"leslie"=>0, "paul"=>1, "maria"=>2}

..This section is all about iterating over data and driving that idea home.

attachments [paper clip]

  last modified size
TXT golfclubs.txt Fri Apr 19 2024 06:52 am 192B
TXT jims_golf.rb Fri Apr 19 2024 06:52 am 1.2K