Skip to content

Instantly share code, notes, and snippets.

@j0ni
Created May 27, 2016 20:37
Show Gist options
  • Save j0ni/7bc313c0429b2b48bdbd5f869a73dd81 to your computer and use it in GitHub Desktop.
Save j0ni/7bc313c0429b2b48bdbd5f869a73dd81 to your computer and use it in GitHub Desktop.
require 'pry'
class Thing
def initialize
@m = {jonathan: 5, richard: 3, anna: 7}
@ks = [:jonathan, :anna, :rikard]
end
def get_count(name)
if @m.include?(name)
@m[name]
else
binding.pry
raise RuntimeError.new("no entry by that key: #{name}")
end
end
def get_counts
@ks.map do |k|
get_count(k)
end
end
end
def main
t = Thing.new
puts t.get_counts
end
main
0 ruby ruby ./20160527-scratch.rb
Frame number: 0/4
From: /Users/joni/Scratch/LighthouseLabs/ruby/20160527-scratch.rb @ line 13 Thing#get_count:
9: def get_count(name)
10: if @m.include?(name)
11: @m[name]
12: else
=> 13: binding.pry
14: raise RuntimeError.new("no entry by that key: #{name}")
15: end
16: end
pry> name
=> :rikard
pry> show-stack -v
Showing all accessible frames in stack (5 in total):
--
=> #0 get_count <Thing#get_count(name)>
in #<Thing> @ ./20160527-scratch.rb:13
#1 [block] block in get_counts <Thing#get_counts()>
in #<Thing> @ ./20160527-scratch.rb:20
#2 [method] get_counts <Thing#get_counts()>
in #<Thing> @ ./20160527-scratch.rb:19
#3 [method] main <Object#main()>
in main @ ./20160527-scratch.rb:27
#4 [main] <main>
in main @ ./20160527-scratch.rb:30
pry> up 1
Frame number: 1/4
Frame type: block
From: /Users/joni/Scratch/LighthouseLabs/ruby/20160527-scratch.rb @ line 20 Thing#get_counts:
18: def get_counts
19: @ks.map do |k|
=> 20: get_count(k)
21: end
22: end
pry> k
=> :rikard
pry> @ks
=> [:jonathan, :anna, :rikard]
pry> continue
NameError: undefined local variable or method `continue' for #<Thing:0x007feb6b010e60>
from (pry):4:in `block in get_counts'
pry> exit
./20160527-scratch.rb:14:in `get_count': no entry by that key: rikard (RuntimeError)
from ./20160527-scratch.rb:20:in `block in get_counts'
from ./20160527-scratch.rb:19:in `map'
from ./20160527-scratch.rb:19:in `get_counts'
from ./20160527-scratch.rb:27:in `main'
from ./20160527-scratch.rb:30:in `<main>'
1 ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment