Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to debug the code to understand how the reduce the values of block passing to the hash length #78

Closed
sadmanahmed opened this issue Jun 15, 2020 · 34 comments

Comments

@sadmanahmed
Copy link

states = ["Kansas","Nebraska", "North Dakota", "South Dakota"]

# lengths: Functional version
def functional_lengths(states)
  states.reduce({}) do |lengths, state|
    lengths[state] = state.length
    lengths
  end
end
puts functional_lengths(states)
#answer : {"Kansas"=>6, "Nebraska"=>8, "North Dakota"=>12, "South Dakota"=>12}
@wasifhossain
Copy link
Contributor

just put binding.pry in the 1st line of the reduce block

@sadmanahmed
Copy link
Author

may I know Exactly where because I tried 3 different place and get this error

functional.rb:113:in `<main>': undefined method `pry' for #<Binding:0x000055718ec50e18> (NoMethodError)

@sadmanahmed
Copy link
Author

ss
the line mentioning would be more helpful

@sadmanahmed
Copy link
Author

Do I need to add require "pry"?

@sadmanahmed
Copy link
Author

def functional_lenghts(states)
  binding.pry
  states.reduce({}) do |lengths,state|

    lengths[state]=state.length
    lengths
  end
end
puts functional_lenghts(states)

error

Traceback (most recent call last):
	1: from functional.rb:122:in `<main>'
functional.rb:115:in `functional_lenghts': undefined method `pry' for #<Binding:0x0000561b91bf0dd0> (NoMethodError)

@wasifhossain
Copy link
Contributor

def functional_lenghts(states)
  binding.pry
  states.reduce({}) do |lengths,state|

👆 thats the 1st line of the functional_lengths method

So

just put binding.pry in the 1st line of the reduce block

would be

def functional_lenghts(states)
  states.reduce({}) do |lengths,state|
    binding.pry

@wasifhossain
Copy link
Contributor

wasifhossain commented Jun 16, 2020

the error points you to require 'pry' before using it. make sure that you have the gem installed

Traceback (most recent call last):
	1: from functional.rb:122:in `<main>'
functional.rb:115:in `functional_lenghts': undefined method `pry' for #<Binding:0x0000561b91bf0dd0> (NoMethodError)

@sadmanahmed
Copy link
Author

But I have used pry in my previous project

@wasifhossain
Copy link
Contributor

how are you running the file? is it part of a rails project or just a standalone file running from the command line

@sadmanahmed
Copy link
Author

Understood .. It is a part of project. I have installed it .

@sadmanahmed
Copy link
Author

I am using a sinatra based project for learning . So I first type this : gem install pry but what should the next command? bundle update/bundle install as typing pry is not working

ruby_totorial git:(master) ✗ pry
bundler: failed to load command: pry (/home/abir/.rvm/gems/ruby-2.6.5/bin/pry)
Gem::Exception: can't find executable pry for gem pry. pry is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
  /home/abir/.rvm/gems/ruby-2.6.5/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
  /home/abir/.rvm/gems/ruby-2.6.5/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
  /home/abir/.rvm/gems/ruby-2.6.5/bin/pry:23:in `<top (required)>'

@wasifhossain
Copy link
Contributor

there is a Gemfile in the project, isn't it? if yes, then you should add pry to that and run bundle install instead of gem install pry

@sadmanahmed
Copy link
Author

bundle install was taking too much time actually that is why I tried to install manually.

@wasifhossain
Copy link
Contributor

ruby projects has own style of running stuffs. we just need to adhere to the rules

@sadmanahmed
Copy link
Author

bundle install taking too much time, is it normal?

@wasifhossain
Copy link
Contributor

around 5 min?

@sadmanahmed
Copy link
Author

still in same place after bundle install started

@wasifhossain
Copy link
Contributor

you can tell bundle install to download in parallel

e.g.

bundle install --jobs=4

would download 4 gems in parallel and hence save some time

@sadmanahmed
Copy link
Author

What is the error as just added pry

 ruby_totorial git:(master) ✗ bundle install
Fetching gem metadata from https://rubygems.org/.......
Resolving dependencies...
Using bundler 2.1.4
Using ruby2_keywords 0.0.2
Using method_source 0.9.2
Fetching coderay 1.1.3
Using rack 2.2.2
Using mustermann 1.1.1
Using tilt 2.0.10
Using rack-protection 2.0.8.1
Using sinatra 2.0.8.1
Installing coderay 1.1.3
Using pry 0.12.2
Bundle complete! 2 Gemfile dependencies, 10 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

error

ruby_totorial git:(master) ✗ ruby test.rb
Traceback (most recent call last):
	4: from test.rb:13:in `<main>'
	3: from test.rb:6:in `functional_lenghts'
	2: from test.rb:6:in `reduce'
	1: from test.rb:6:in `each'
test.rb:7:in `block in functional_lenghts': undefined method `pry' for #<Binding:0x0000557a527a4cf8> (NoMethodError)

ss

@wasifhossain
Copy link
Contributor

can you retry after uncommenting line 1 (require 'pry')

@sadmanahmed
Copy link
Author

ruby_totorial git:(master) ✗ ruby test.rb
Traceback (most recent call last):
	4: from test.rb:13:in `<main>'
	3: from test.rb:6:in `functional_lenghts'
	2: from test.rb:6:in `reduce'
	1: from test.rb:6:in `each'
test.rb:7:in `block in functional_lenghts': undefined method `pry' for #<Binding:0x000055c2a74dc8c0> (NoMethodError)
➜  ruby_totorial git:(master) ✗ ruby test.rb
Traceback (most recent call last):
	2: from test.rb:1:in `<main>'
	1: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- pry (LoadError)
	20: from test.rb:1:in `<main>'
	19: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:34:in `require'
	18: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `rescue in require'
	17: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `require'
	16: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry.rb:78:in `<top (required)>'
	15: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	14: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	13: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/cli.rb:134:in `<top (required)>'
	12: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/cli.rb:40:in `add_plugin_options'
	11: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/cli.rb:40:in `each'
	10: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/plugins.rb:55:in `load_cli_options'
	 9: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 8: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 7: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.7.0/lib/pry-byebug/cli.rb:5:in `<top (required)>'
	 6: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 5: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 4: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.7.0/lib/pry-byebug/commands.rb:12:in `<top (required)>'
	 3: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 2: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 1: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.7.0/lib/pry-byebug/commands/exit_all.rb:3:in `<top (required)>'
/home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.7.0/lib/pry-byebug/commands/exit_all.rb:7:in `<module:PryByebug>': uninitialized constant Pry::Command::ExitAll (NameError)
➜  ruby_totorial git:(master) ✗ 

@wasifhossain
Copy link
Contributor

can we see the Gemfile

@sadmanahmed
Copy link
Author

source 'https://rubygems.org'
gem 'sinatra'
gem 'pry', '~> 0.12.2'

@wasifhossain
Copy link
Contributor

can you share the output of gem list pry

@sadmanahmed
Copy link
Author

ruby_totorial git:(master) ✗ gem list pry  

*** LOCAL GEMS ***

pry (0.13.1, 0.13.0, 0.12.2)
pry-byebug (3.7.0)
pry-rails (0.3.9)

@wasifhossain
Copy link
Contributor

and Gemfile.lock

@sadmanahmed
Copy link
Author

GEM
  remote: https://rubygems.org/
  specs:
    coderay (1.1.3)
    method_source (0.9.2)
    mustermann (1.1.1)
      ruby2_keywords (~> 0.0.1)
    pry (0.12.2)
      coderay (~> 1.1.0)
      method_source (~> 0.9.0)
    rack (2.2.2)
    rack-protection (2.0.8.1)
      rack
    ruby2_keywords (0.0.2)
    sinatra (2.0.8.1)
      mustermann (~> 1.0)
      rack (~> 2.0)
      rack-protection (= 2.0.8.1)
      tilt (~> 2.0)
    tilt (2.0.10)

PLATFORMS
  ruby

DEPENDENCIES
  pry (~> 0.12.2)
  sinatra

BUNDLED WITH
   2.1.4

@wasifhossain
Copy link
Contributor

could you add pry-byebug too in the Gemfile, run bundle install and retry test.rb if you can overcome the error

@sadmanahmed
Copy link
Author

 ruby_totorial git:(master) ✗ bundle install
Fetching source index from https://rubygems.org/
Resolving dependencies....
Using bundler 2.1.4
Using method_source 0.9.2
Using coderay 1.1.3
Using ruby2_keywords 0.0.2
Using tilt 2.0.10
Using pry 0.12.2
Using mustermann 1.1.1
Fetching byebug 11.1.3
Using rack 2.2.2
Using rack-protection 2.0.8.1
Using sinatra 2.0.8.1
Installing byebug 11.1.3 with native extensions
Fetching pry-byebug 3.8.0
Installing pry-byebug 3.8.0
Bundle complete! 3 Gemfile dependencies, 12 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
➜  ruby_totorial git:(master) ✗ ruby test.rb  
Traceback (most recent call last):
	2: from test.rb:1:in `<main>'
	1: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- pry (LoadError)
	20: from test.rb:1:in `<main>'
	19: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:34:in `require'
	18: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `rescue in require'
	17: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `require'
	16: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry.rb:78:in `<top (required)>'
	15: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	14: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	13: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/cli.rb:134:in `<top (required)>'
	12: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/cli.rb:40:in `add_plugin_options'
	11: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/cli.rb:40:in `each'
	10: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/plugins.rb:55:in `load_cli_options'
	 9: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 8: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 7: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.8.0/lib/pry-byebug/cli.rb:5:in `<top (required)>'
	 6: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 5: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 4: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.8.0/lib/pry-byebug/commands.rb:12:in `<top (required)>'
	 3: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 2: from /home/abir/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 1: from /home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.8.0/lib/pry-byebug/commands/exit_all.rb:3:in `<top (required)>'
/home/abir/.rvm/gems/ruby-2.6.5/gems/pry-byebug-3.8.0/lib/pry-byebug/commands/exit_all.rb:7:in `<module:PryByebug>': uninitialized constant Pry::Command::ExitAll (NameError)

@wasifhossain
Copy link
Contributor

source 'https://rubygems.org'
gem 'sinatra'
gem 'pry', '~> 0.12.2'

change pry version to 0.13.1, like

gem 'pry', '~> 0.13.1'

run bundle install and bundle update pry-byebug too. then give test.rb one more try

Ref: pry/pry#2121 (comment)

@sadmanahmed
Copy link
Author

sadmanahmed commented Jun 18, 2020

vaia
It is working but now what to do to see the structure?

ruby_totorial git:(master) ✗ ruby test.rb  

From: /home/abir/repos/ruby_totorial/test.rb:9 Object#functional_lenghts:

     5: def functional_lenghts(states)
     6:   states.reduce({}) do |lengths,state|
     7:    binding.pry
     8: 
 =>  9:     lengths[state]=state.length
    10:     lengths
    11:   end
    12: end

[1] pry(main)> 

I want to see how the datas are processing and holding the values again in a hash function

@wasifhossain
Copy link
Contributor

just check the value of the variables inside the current scope, e.g lengths, state etc

in order to jump to the next iteration, use exit or ctrl+D.

@sadmanahmed
Copy link
Author

ruby_totorial git:(master) ✗ ruby test.rb  

From: /home/abir/repos/ruby_totorial/test.rb:9 Object#functional_lenghts:

     5: def functional_lenghts(states)
     6:   states.reduce({}) do |lengths,state|
     7:    binding.pry
     8: 
 =>  9:     lengths[state]=state.length
    10:     lengths
    11:   end
    12: end

[1] pry(main)> state = Kansas
NameError: uninitialized constant Kansas
from (pry):1:in `block in functional_lenghts'
[2] pry(main)> state =" Kansas"
=> " Kansas"
[3] pry(main)> 
[4] pry(main)> 

From: /home/abir/repos/ruby_totorial/test.rb:9 Object#functional_lenghts:

     5: def functional_lenghts(states)
     6:   states.reduce({}) do |lengths,state|
     7:    binding.pry
     8: 
 =>  9:     lengths[state]=state.length
    10:     lengths
    11:   end
    12: end

[1] pry(main)> 

From: /home/abir/repos/ruby_totorial/test.rb:9 Object#functional_lenghts:

     5: def functional_lenghts(states)
     6:   states.reduce({}) do |lengths,state|
     7:    binding.pry
     8: 
 =>  9:     lengths[state]=state.length
    10:     lengths
    11:   end
    12: end

[1] pry(main)> 

From: /home/abir/repos/ruby_totorial/test.rb:9 Object#functional_lenghts:

     5: def functional_lenghts(states)
     6:   states.reduce({}) do |lengths,state|
     7:    binding.pry
     8: 
 =>  9:     lengths[state]=state.length
    10:     lengths
    11:   end
    12: end

[1] pry(main)> 
{" Kansas"=>7, "Nebraska"=>8, "North Dakota"=>12, "South Dakota"=>12}

Maybe I am doing something wrong .

@sadmanahmed
Copy link
Author

Is this work something like this?

ruby_totorial git:(master) ✗ ruby test.rb

From: /home/abir/repos/ruby_totorial/test.rb:9 Object#functional_lenghts:

     5: def functional_lenghts(states)
     6:   states.reduce({}) do |lengths,state|
     7:    binding.pry
     8: 
 =>  9:     lengths[state]=state.length
    10:     lengths
    11:   end
    12: end

[1] pry(main)> state
=> "Kansas"
[2] pry(main)> lengths
=> {}
[3] pry(main)> state.length
=> 6
[4] pry(main)> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants