-1

I'm trying to develop a ruby gem for practice and I'm wondering how do I require pry during development and test runs? Is there anyway to require the gem only during development? I'm on Ruby and not Rails and I don't think I have any environment variables setup to rely on. Is there a conventional way to do this?

enter image description here

and

enter image description here

Currently if I run code that hits the above line, I get this error:

NoMethodError: undefined method `pry' for #<Binding:0x007f8d3287c4d8>
    from /Users/jwan/programming/interview_questions/gemini/jobcoin_client/lib/jobcoin_client/requests/connection.rb:18:in `post'

A few questions:

  1. How do I properly require pry so this line doesn't throw an error when developing a gem?

  2. I read Yahuda's post but I'm still unclear why adding dependencies in the gemspec vs adding dependencies in the Gemfile. What is the difference?

Currently, after I make changes to the ruby gem, I have to run these series of commands. Is there anything more efficient that I can do?

 gem build jobcoin_client.gemspec
WARNING:  no homepage specified
WARNING:  open-ended dependency on pry (>= 0, development) is not recommended
  if pry is semantically versioned, use:
    add_development_dependency 'pry', '~> 0'
WARNING:  See http://guides.rubygems.org/specification-reference/ for help
  Successfully built RubyGem
  Name: jobcoin_client
  Version: 0.1.0
  File: jobcoin_client-0.1.0.gem

$ gem install jobcoin_client
Successfully installed jobcoin_client-0.1.0
Parsing documentation for jobcoin_client-0.1.0
Done installing documentation for jobcoin_client after 0 seconds
1 gem installed
 05:45 PM   
$ irb
irb(main):001:0> require 'jobcoin_client'
=> true
irb(main):002:0> require 'pry'
=> false
Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • 1
    Possible duplicate of [How to add "pry" when developing a Ruby gem](https://stackoverflow.com/questions/18335229/how-to-add-pry-when-developing-a-ruby-gem) – max pleaner Jan 23 '18 at 22:45
  • what about installing it to the system (`gem install`) and removing the pry-related stuff (require, breakpoint) before building for distribution? – max pleaner Jan 23 '18 at 22:49

1 Answers1

0

You can install it to the system and use a boolean flag to conditionally require it and set breakpoints.

gem install pry

Then in code, something like this:

SET_BREAKPOINTS = ENV["SET_BREAKPOINTS"] == "true"

require 'pry' if SET_BREAKPOINTS

binding.pry if SET_BREAKPINTS

To turn breakpoints on, you can manipulate env through code:

ENV["SET_BREAKPOINTS"] = "true"

or when calling a script from bash:

env SET_BREAKPOINTS=true irb -r 'your_gem' 
max pleaner
  • 26,189
  • 9
  • 66
  • 118