Jim's
Tutorials

Fall 2018
course
site

Github: https://github.com/LeslieWilson/marlboro_rails

Rails Week 1

Interactive console- IRB and Pry [09/07/2018]

Did a command line refresher, installed the ruby debugger called "pry" and paused execution of some code with binding.pry – got to verify the values of these variables as practice (see image 1). Basically, pry works when you put a "require pry" placeholder in your code and then inject a "binding.pry" statement in whatever location you'd like everything to "freeze" and where you'd like to further investigate what processes are happening/where something may have gone wrong.

I also learned about ruby gems (which are snippets of code you can use, pry is just one gem) by watching “understanding pry and ruby gems.” This was a 20 minute video launch academy offers. I messed around with string replacement and changing up variables (image 2). I also read some of the documentation for strings and messed around with some of the examples from this: String Documentation

I used pry in a loop that would occur a random number of times, tried to get out of it, and had to hit exit 5 times to because I'd put the binding.pry within it. Took me a second to figure out why I couldn't get out. Then I made a loop that is a dice rolling game and used "puts" statements as another way to debug and reveal what's happening in the code. That is, you can print specific information rather than something that generalizes over processes, makes everything more transparent. Anyway, I made my dice-rolling game to loop over and over again until the two die values came out the same (image 3, 4, 5,6).

Tried putting in a pry after the dieroll. I tried running it and got doubles the first time, and this didn't work. I think what happened is, my code never "entered the racetrack" of the loop so pry wasn't triggered. The second time it did show up (image 7) because I didn't randomly manage to get doubles the first time. I checked to see what the values of my first and second die rolls were, it's true they were different. Then I tried to force the condition that they were the same, and got it to say “doubles!”. So I changed the value of my variable and forced myself out of the loop. Finally I tried to add a condition to the binding.pry, and used flow control to conditionally debug (image 8)

Conditionals and flow control [09/10/2018]

Today I watched several videos conditionals and flow control and practiced using pry in this. Specifically made a loop that took a user input and evaluated whether it was odd or not to give a specific output. learned the input_to.i trick that would allow a string to be evaluated as an integer. Also made a loop that took inputs of different names, accepting some and not others. It was interesting to use pry to interrupt these conditional statements.

From: /Users/lesliewilson/marlboro_rails/street-show/code.rb @ line 36 :

    31:
    32: require 'pry'
    33: print "whats your lucky number? "
    34: input= gets.chomp
    35:
    36: binding.pry
    37:
    38: if input.to_i % 2 == 0
    39:     puts "that's a great lucky number"
    40: else
    41:     puts "not lucky"

..and it worked. Simple but nice to review (see image 9).

I also made a dice-rolling game that uses rand (which I checked out w/pry)

puts "would you like to roll the dice? "
input = gets.chomp
if input == "yes"
    dice1 = rand(6) + 1
    dice2 = rand(6) + 1
    total = dice1 + dice2
    print "you rolled : "
    puts total
else
    puts "ok I won't"
end

Did a little exercise that worked fine:

dinner_total = 178
tip_expected = 0.2
tip = dinner_total*tip_expected

puts "you should tip #{tip_expected}"
puts "the total bill is #{dinner_total}"
puts "your tip then is #{tip}"

attachments [paper clip]

  last modified size
TXT Image1.png Wed Dec 04 2024 08:39 am 563K
TXT Image2.png Wed Dec 04 2024 08:39 am 331K
TXT Image3.png Wed Dec 04 2024 08:39 am 280K
TXT Image4.png Wed Dec 04 2024 08:39 am 195K
TXT Image5.png Wed Dec 04 2024 08:39 am 188K
TXT Image6.png Wed Dec 04 2024 08:39 am 249K
TXT Image7.png Wed Dec 04 2024 08:39 am 369K
TXT Image8.png Wed Dec 04 2024 08:39 am 95K
TXT Image9.png Wed Dec 04 2024 08:39 am 82K