Jim's
Tutorials

Fall 2018
course
site

Rails Week 3

mixing rand with loops

So I'm now moving into multi-part little apps, here's one where if you put in a specific number of items you want the "Vending machine" will spit the name out that number of times, and if I don't specify it will give me a random number of things. This one actually took some thinking in terms of, how do I differentiate between the two different inputs and what they mean, how to add them in one instance and treat them as a string the next- etc.

puts "hi, what do you want today"
input_1 = gets.chomp
puts "how many do you want"
input_2 = gets.chomp
sum = input_2.to_i
random = rand(14)+1


if input_2 == "tons"
print "#{input_1}\n" * random
elsif sum != "tons"
    print "#{input_1}\n" * sum
puts "ok! come again"

end

..And this is the output:

hi, what do you want today
chips
how many do you want
5
chips
chips
chips
chips
chips
ok! come again
➜  machine-communication git:(master) ✗ ruby test.rb
hi, what do you want today
chips
how many do you want
tons
chips
chips
chips

then I changed a bit of the language so that anyone could say a sentence with "tons" in it, and you'd get a random number of that item generated.

if input_2.index('tons') != nil

..overall this was a good exercise in what I've already learned.

I decided to something a little more challenging with loops and found a cool 99 bottles of beer exercise. I had some interesting problems when I didin't start "base" at 100.. like it would print 99 bottles, go to 98, then go to like, 1 and 2 bottle.. and end up at -2 bottles .. I struggled for a second about where to put things in the while loop/outside of it but figured it out in the end. Yeah was interesting having to stagger the values of the variables against each other.

base = 100
start_num = base - 1
end_num = start_num - 1


puts "start to start"
start = gets.chomp

while start != nil and start_num !=0 and start_num < 100
    start_num = base - 1
    end_num = start_num -1
    base = base - 1
puts  "#{start_num} bottles of beer on the wall, #{start_num} bottles of beer, if one falls down then pass it around #{end_num} bottles of beer"

end

and the output for 99 bottles (at the end):

3 bottles of beer on the wall, 3 bottles of beer, if one falls down then pass it around 2 bottles of beer
2 bottles of beer on the wall, 2 bottles of beer, if one falls down then pass it around 1 bottles of beer
1 bottles of beer on the wall, 1 bottles of beer, if one falls down then pass it around 0 bottles of beer
0 bottles of beer on the wall, 0 bottles of beer, if one falls down then pass it around -1 bottles of beer
➜  ninety-nine-bottles git:(master) ✗

This exercise took a lot of pry sessions that I won't illustrate because I guess pry isn't really a debugger? but it took me constantly having to check the values of my start_num and end_num variables because for a minute there end_num wasn't being registered in the song as being one less...pry was helpful in double checking that.

Then I moved onto arrays and read this official ruby array documentation to kick myself off and so I also watched a half hour video I can't really post here about arrays. I tried running some arrays in IRB and had trouble like, verifying things with that so read some of this stuff about exploring Interactive Ruby and why you'd want to do it.

eventually got a chance to play around with arrays in IRB, did little exercises like this to get a feel for it

irb(main):002:0> array_ = ["dog","cat"]
=> ["dog", "cat"]
irb(main):003:0> array_[1]
=> "cat"
irb(main):004:0>

More Practice with Arrays

I went through the ruby array documentation and tried out every method it gave me. Then I had nate quiz me. Here is sample output of my exercises:


irb(main):001:0> ary = [1, "two", 3.0]
=> [1, "two", 3.0]
irb(main):002:0> ary = Array.new
=> []
irb(main):003:0> Array.new(3)
=> [nil, nil, nil]
irb(main):004:0> Array.new(3,"horse")
=> ["horse", "horse", "horse"]
irb(main):005:0> Array.new(4) {Hash.new}
=> [{}, {}, {}, {}]
irb(main):006:0> empty_table = Array.new(3) {Array.new(3,"cat")}
=> [["cat", "cat", "cat"], ["cat", "cat", "cat"], ["cat", "cat", "cat"]]
irb(main):007:0> Array({:a => "a", :b => "b"})
=> [[:a, "a"], [:b, "b"]]
irb(main):008:0> arr = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):009:0> arr[2]
=> 3
irb(main):010:0> arr[100]
=> nil
irb(main):011:0> arr[-3]
=> 4
irb(main):012:0> arr[2,3]
=> [3, 4, 5]
irb(main):013:0> arr[1..4]
=> [2, 3, 4, 5]
irb(main):014:0> arr.at(0)
=> 1
irb(main):015:0> arr = ['a','b','c','d','e','f']
=> ["a", "b", "c", "d", "e", "f"]
irb(main):016:0> arr.fetch(100)
IndexError: index 100 outside of array bounds: -6...6
    from (irb):16:in `fetch'
    from (irb):16
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):017:0> arr.fetch(100, "oops")
=> "oops"
irb(main):018:0> arr.fetch(2,"oops")
=> "c"
irb(main):019:0> arr.first
=> "a"
irb(main):020:0> arr.last
=> "f"
irb(main):021:0> arr.drop(3)
=> ["d", "e", "f"]
irb(main):022:0> arr.take(3)
=> ["a", "b", "c"]
irb(main):023:0> arr
=> ["a", "b", "c", "d", "e", "f"]
irb(main):024:0>
irb(main):025:0* arr
=> ["a", "b", "c", "d", "e", "f"]
irb(main):026:0> browsers = ['Chrome','Firefox','Safari','Opera','IE']
=> ["Chrome", "Firefox", "Safari", "Opera", "IE"]
irb(main):027:0> browsers.length
=> 5
irb(main):028:0> browsers.count
=> 5
irb(main):029:0> browsers.empty?
=> false
irb(main):030:0> browsers.include?('konqueror')
=> false
irb(main):031:0> arr = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):032:0> arr.push(5)
=> [1, 2, 3, 4, 5]
irb(main):033:0> arr << 6
=> [1, 2, 3, 4, 5, 6]
irb(main):034:0> arr >> 0
NoMethodError: undefined method `>>' for [1, 2, 3, 4, 5, 6]:Array
    from (irb):34
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):035:0> arr.unshift(0)
=> [0, 1, 2, 3, 4, 5, 6]
irb(main):036:0> arr.insert(3,'apple')
=> [0, 1, 2, "apple", 3, 4, 5, 6]
irb(main):037:0> arr.insert(3,'orange','pear','grapefruit')
=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
irb(main):038:0> arr = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):039:0> arr.pop
=> 6
irb(main):040:0> arr
=> [1, 2, 3, 4, 5]
irb(main):041:0> arr.shift
=> 1
irb(main):042:0> arr
=> [2, 3, 4, 5]
irb(main):043:0> arr.delete_at(2)
=> 4
irb(main):044:0> arr = ['foo',0,nil,'bar',7,'baz',nil]
=> ["foo", 0, nil, "bar", 7, "baz", nil]
irb(main):045:0> arr.compact
=> ["foo", 0, "bar", 7, "baz"]
irb(main):046:0> arr
=> ["foo", 0, nil, "bar", 7, "baz", nil]
irb(main):047:0> arr.compact!
=> ["foo", 0, "bar", 7, "baz"]
irb(main):048:0> aff
NameError: undefined local variable or method `aff' for main:Object
    from (irb):48
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):049:0> arr
=> ["foo", 0, "bar", 7, "baz"]
irb(main):050:0> arr = [2,5,6,566,6,6,8,9,0,123,556]
=> [2, 5, 6, 566, 6, 6, 8, 9, 0, 123, 556]
irb(main):051:0> arr.uniq
=> [2, 5, 6, 566, 8, 9, 0, 123, 556]
irb(main):052:0> arr
=> [2, 5, 6, 566, 6, 6, 8, 9, 0, 123, 556]
irb(main):053:0> arr.uniq
=> [2, 5, 6, 566, 8, 9, 0, 123, 556]
irb(main):054:0> arr.uniq!
=> [2, 5, 6, 566, 8, 9, 0, 123, 556]
irb(main):055:0> arr
=> [2, 5, 6, 566, 8, 9, 0, 123, 556]
irb(main):056:0> arr = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):057:0> arr.each {|a| print -= 10, ""}
SyntaxError: (irb):57: syntax error, unexpected ',', expecting '}'
arr.each {|a| print -= 10, ""}
                          ^
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):058:0> arr.each {|a| print -= 10, " "}
SyntaxError: (irb):58: syntax error, unexpected ',', expecting '}'
arr.each {|a| print -= 10, " "}
                          ^
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):059:0> arr.each {|a| print a  -= 10, " "}
-9 -8 -7 -6 -5 => [1, 2, 3, 4, 5]
irb(main):060:0> words = %w[rats live on no evil star]
=> ["rats", "live", "on", "no", "evil", "star"]
irb(main):061:0> str = ""
=> ""
irb(main):062:0> words.reverse_each {|word| str += "#{word.reverse} "}
=> ["rats", "live", "on", "no", "evil", "star"]
irb(main):063:0> str
=> "rats live on no evil star "
irb(main):064:0> arr
=> [1, 2, 3, 4, 5]
irb(main):065:0> arr.map { |a| 2*a }
=> [2, 4, 6, 8, 10]
irb(main):066:0> arr
=> [1, 2, 3, 4, 5]
irb(main):067:0> arr.map!{|a| a**2}
=> [1, 4, 9, 16, 25]
irb(main):068:0> arr
=> [1, 4, 9, 16, 25]
irb(main):069:0> arr = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):070:0> arr.select {|a| a > 3}
=> [4, 5, 6]
irb(main):071:0> arr.reject { |a| a<3 }
=> [3, 4, 5, 6]
irb(main):072:0> arr.drop_while { |a| a < 4}
=> [4, 5, 6]
irb(main):073:0> arr.delete_if { |a| a<4}
=> [4, 5, 6]
irb(main):074:0> arr
=> [4, 5, 6]
irb(main):075:0> arr = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):076:0> arr.keep_if{ |a| a < 4}
=> [1, 2, 3]
irb(main):077:0> arr
=> [1, 2, 3]
irb(main):078:0> Array.[](1,'a',/^A/)
=> [1, "a", /^A/]
irb(main):079:0> Array
=> Array
irb(main):080:0> Array.include?(1)
TypeError: wrong argument type Fixnum (expected Module)
    from (irb):80:in `include?'
    from (irb):80
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):081:0> Array.include?[1]
ArgumentError: wrong number of arguments (given 0, expected 1)
    from (irb):81:in `include?'
    from (irb):81
    from /Users/lesliewilson/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
irb(main):082:0> Array["leslie",3,5]
=> ["leslie", 3, 5]
irb(main):083:0> ["mom","dad","chris"]
=> ["mom", "dad", "chris"]
irb(main):084:0> Array.new(4) {|index| index ** 3}
=> [0, 1, 8, 27]
irb(main):085:0> Array.new(2) {|index| index ** 2}
=> [0, 1]
irb(main):086:0> a = Array.new(2, Hash.new)
=> [{}, {}]
irb(main):087:0> a[0]['cat'] = 'feline'
=> "feline"
irb(main):088:0> a
=> [{"cat"=>"feline"}, {"cat"=>"feline"}]
irb(main):089:0> a[1]['cat'] = 'felix'
=> "felix"
irb(main):090:0> a
=> [{"cat"=>"felix"}, {"cat"=>"felix"}]
irb(main):091:0> a = Array.new(2) {Hash.new}
=> [{}, {}]
irb(main):092:0> a[0]['cat'] = 'feline'
=> "feline"
irb(main):093:0> a
=> [{"cat"=>"feline"}, {}]
irb(main):094:0> Array.try_convert([1])
=> [1]
irb(main):095:0> Array.try_convert("1")
=> nil
irb(main):096:0> if tmp = Array.try_convert([1])
irb(main):097:1> String.try_convert("leslie")
irb(main):098:1>
irb(main):099:1*
irb(main):100:1* array
irb(main):101:1> String.try_convert("leslie")
irb(main):102:1> Array.try_convert("1")
irb(main):103:1> exit
irb(main):104:1> exit
irb(main):105:1> quit
irb(main):106:1> ^C
irb(main):106:0> String.try_convert("leslie")
=> "leslie"
irb(main):107:0> String.try_convert([1])
=> nil
irb(main):108:0> ["leslie", "chris", "mom"] & ["leslie", "larisa", "nate"]
=> ["leslie"]
irb(main):109:0> [1,2,3] * 3
=> [1, 2, 3, 1, 2, 3, 1, 2, 3]
irb(main):110:0> [1,2,3] * ","
=> "1,2,3"
irb(main):111:0> [1,2,3] * "[,]"
=> "1[,]2[,]3"
irb(main):112:0> [1,2,3] + [4,5]
=> [1, 2, 3, 4, 5]
irb(main):113:0> a = ["a", "b", "c"]
=> ["a", "b", "c"]
irb(main):114:0> a + ["e", "f"]
=> ["a", "b", "c", "e", "f"]
irb(main):115:0>
irb(main):116:0* a
=> ["a", "b", "c"]
irb(main):117:0> a
=> ["a", "b", "c"]
irb(main):118:0> a
=> ["a", "b", "c"]
irb(main):119:0> a + ["e", "f"]
=> ["a", "b", "c", "e", "f"]
irb(main):120:0> a
=> ["a", "b", "c"]
irb(main):121:0> [1,2,1,1,4,5] - [1,2]
=> [4, 5]
irb(main):122:0> [1,2] << "c" << "d" << [3,4]
=> [1, 2, "c", "d", [3, 4]]

Looping through arrays

ary = ["leslie", 'nate', 'dog']
i = 0
while i < array.length
    print array[i]
    i +=1

end

and

puts "my to do list"
to_do = ["buy milk", "learn arrays", "learn ruby"]

to_do.each do |item|
    print "YES"
    puts item
end

and


puts "my list of people i like"
array_1 = ['michele', 'paul', 'mike']
i = 0
while i < array_1.length
    print "*"
    puts array_1[i]
    i += 1
end

QUIZ

...Nate quizzed me from 7:00pm-8:00pm on September 25th on my array practice and I got about 80%.