Jim's
Tutorials

Fall 2018
course
site

Testing Week 4

More pry/ pry with more complicated apps

So, even though technically pry is not a 'real' debugger as one might think of it, I've had to use it more and more as apps get more complicated in the ruby curriculum. Here's a fairly interesting use of it as I try to make a "shopping cart" that performs various actions depending on what someone tries to put into it. I'll detail a few things I learned along the way.

The idea is to make a shopping cart that only allows you to put in specific, predetermined items, and then the cart needs to save that. If they type "finished" they are done putting things in the cart and need to stop being prompted.

Here's what I made initially, which was wrong:


require 'pry'

possible_items = ['old paperback', 'potato', 'red onion', 'dried lemon', 'medicinal herbs', 'saffron']
new_list = []


puts "what do you want in your cart? type 'finished' to finish"
pick = gets.chomp


while possible_items.index(pick) != nil
    puts "that works! anything else? hit 'finished' to finish"
    pick = gets.chomp
    new_list << pick
if possible_items.index(pick) == nil && pick != 'finished'
    puts "that's not on the list"
    pick = gets.chomp
    new_list << pick

    binding.pry

end
end

...and here's the output:

➜  arrays git:(master) ✗ ruby arrays.rb
what do you want in your cart? type 'finished' to finish
potato
that works! anything else? hit 'finished' to finish
potato
that works! anything else? hit 'finished' to finish
happy
that's not on the list
finished

and here's the pry, which is the interesting part:

[1] pry(main)> new_list
=> ["potato", "happy", "finished"]
[2] pry(main)> pick
=> "finished"
[3] pry(main)> pick
=> "finished"
[4] pry(main)> possible_item
NameError: undefined local variable or method `possible_item' for main:Object
Did you mean?  possible_items
from (pry):4:in `<main>'
[5] pry(main)> possible_items
=> ["old paperback", "potato", "red onion", "dried lemon", "medicinal herbs", "saffron"]
[6] pry(main)>

SO.. basically my code is not working because no matter what I "pick" it's being added to the cart. even finished is being added.

Clearly I'm having trouble wrangling these arrays..

so, trying again, and this is really, really not working- it's long, it repeats itself, and I'm having trouble getting the right things to appear in my list:

require 'pry'

possible_items = ['old paperback', 'potato', 'red onion', 'dried lemon', 'medicinal herbs', 'saffron']
new_list = []


puts "what do you want in your cart? type 'finished' to finish"
pick = gets.chomp
new_list << pick

while possible_items.index(pick) != nil
    puts "that works! anything else? hit 'finished' to finish"
    pick = gets.chomp
    new_list << pick

if possible_items.index(pick) == nil
    puts "that's not on the list. select again?"
    new_list.delete(pick)
    pick = gets.chomp
    new_list << pick
    binding.pry
if pick == 'finished'
    break
end
end
end

...this is a complete mess. here is the output- you can see the first time it "appears" to have accepted only the acceptable items onto the list, but then it adds "finished" in there sneakily which is totally wrong:

[1] pry(main)> new_list
=> ["potato", "saffron", "red onion"] --------------- Here!!
[2] pry(main)> exit
that works! anything else? hit 'finished' to finish
finished
that's not on the list. select again?
finished

From: /Users/lesliewilson/marlboro_rails/arrays/arrays.rb @ line 22 :

    17: if possible_items.index(pick) == nil
    18:     puts "that's not on the list. select again?"
    19:     new_list.delete(pick)
    20:     pick = gets.chomp
    21:     new_list << pick
 => 22:     binding.pry
    23: if pick == 'finished'
    24:     break
    25: end
    26: end
    27: end

[1] pry(main)> new_list
=>["potato", "saffron", "red onion", "finished"] ----------------   HERE!!
[2] pry(main)>

straight up Rspec

Spent time reading the documentation about rspec-coreand expectations and got started on the rails section of the launch curriculum.

They start us on a very simple test.

require_relative './code'

describe 'plus_one' do
it 'should be one plus the number given' do
result = plus_one(0)
expect (result).to eq(1)

end
end