Why does require choose the latest version of a gem? - ruby

If I load a gem, let's say activerecord, in IRB require chooses the latest version of activerecord. Programmatically, what is influencing this decision to choose the latest gem version? Is require doing this, or is there something in the loaded IRB that forces requires to choose the latest version?
Here are my activerecord gems installed by bundler:
➜ ~ ls -al /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activere
activerecord-4.2.10/ activerecord-5.0.0.1/ activerecord-5.1.2/ activerecord-5.1.3/ activerecord-5.1.4/ activerecord-5.1.5/ activerecord-5.1.6/ activeresource-5.0.0/
And here is an example IRB session:
➜ ~ irb
2.4.1 :001 > require 'active_record'
=> true
2.4.1 :002 > Gem.loaded_specs['activerecord'].version
=> #<Gem::Version "5.1.6">
2.4.1 :003 >

If I load a gem, let's say activerecord, in IRB require chooses the latest version of activerecord.
Actually, it chooses the latest version that doesn't conflict with any already activated gem.
Programmatically, what is influencing this decision to choose the latest gem version? Is require doing this, or is there something in the loaded IRB that forces requires to choose the latest version?
This is requires job. More specifically, it is the job of the monkey-patched require from the RubyGems library, not the original require from the Ruby core library.
This is just simple separation of concerns: IRb is a REPL, not a package management system, it shouldn't know anything about packages.

Related

How to require pry during development of a ruby gem?

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?
and
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:
How do I properly require pry so this line doesn't throw an error when developing a gem?
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
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'

Which version of gem is used when there is no Gemfile

There is similar question here.
Ruby: Rails: Which version of a gem is used?
But it's about when Gemfile is used.
I want to know if there is no Gemfile, which version of gem is used.
For example I have 4 versions of selenium-webdriver in my system.
% gem list | grep selenium
selenium-webdriver (2.53.0, 2.48.1, 2.46.2, 2.45.0)
And I just use it by pry and require 'selenium-webdriver. How can I know which version is used? Only the latest is selected?
You probably have more than one gem because each project with Gemfile in your machine have one different version of it.
When using the gem via require or command line without specifying the version the last one - the greater version - will be automatically used.
By convention, in the most part of the cases, you can print the version doing the following:
require 'some_gem'
puts SomeGem::VERSION
# => "3.0.3"

Problems with ruby 'unroller' gem

I'm having a problem using unroller. I have installed the gem and wrote this simple program to help focus on the problem i'm having:
#!/usr/bin/ruby
require 'rubygems'
require 'unroller'
Unroller::trace
def foo(p1, p2)
puts p1
puts p2
end
foo("param1", "param2")
Running the program yields:
/Library/Ruby/Gems/1.8/gems/facets-2.9.3/lib/core/facets/filetest/separator_pattern.rb:5: warning: already initialized constant SEPARATOR_PATTERN
/Library/Ruby/Gems/1.8/gems/facets-2.9.3/lib/core/facets/string/bracket.rb:3: warning: already initialized constant BRA2KET
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- facets/methodspace (LoadError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from /Library/Ruby/Gems/1.8/gems/unroller-1.0.0/lib/unroller.rb:4
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `require'
from ./ut:4
My ruby version is ruby 1.8.7 (2011-12-28 patchlevel 357). I also installed ruby on my Windows development box and get the same error and that ruby version is 1.9.3 so it does not appear to be related to the version of Ruby I'm on.
Anyone have any ideas?
Thanks very much in advance!
jon
This is a bug of unroller gem, described here: https://github.com/TylerRick/unroller/issues/1. unroller automatically requires the latest version of facets gem and the version 2.9 breaks it. (BTW gems should never use '>=' when loading dependencies, that's why '~>' is for.)
It's not that difficult to hotfix locally by using bundler and hardcoding facets gem to specific version before requiring unroller (so the specific facets version gets loaded instead of latest 2.9).
create Gemfile:
source 'http://rubygems.org'
gem 'facets', '2.8.4'
gem 'termios' # you're gonna need this gem too, for some reason
gem 'unroller'
run bundle install and then either run the script by bundle exec ruby test.rb or require bundler/setup in it:
require 'rubygems'
require 'bundler/setup'
require 'unroller'
...
UPDATE: or if you don't wanna deal with bundler, try this first, it could work too:
require 'rubygems'
gem 'facets', '2.8.4'
require 'unroller'
...

Can't stop Sinatra using Ctrl+C when `require 'coffee-script'`

I'm running Ruby 1.9.2p180 on Ubuntu 11.04 64-bit.
I have the simplest possible CoffeeScript example using Sinatra which runs fine, but doesn't stop when I press the Ctrl+C shortcut. So every time I have to kill -9 and it's getting quite tedious.
The app.rb:
require 'sinatra'
require 'coffee-script'
get '/' do
'<script src="/app.js" type="text/javascript"></script>'
end
get '/app.js' do
coffee :app
end
The views/app.coffee:
alert 'Foo'
It works on Ruby 1.8.7 with minor modifications:
require 'rubygems'
require 'sinatra'
require 'coffee-script'
require 'json'
get '/' do
'<script src="/app.js" type="text/javascript"></script>'
end
get '/app.js' do
coffee :app
end
It also works on 1.9.2 when I remove the line require 'coffee-script', but gives me a warning:
WARN: tilt autoloading 'coffee_script' in a non thread-safe way; explicit require 'coffee_script' suggested.
Figured out that it works when using therubyracer, but fails on node. The version 0.1.97 from Ubuntu 10.10 repositories is the only exception. When using the 0.2.6 from Natty, it fails and also when installing the latest (0.4.8) using nvm, I wasn't able to install 0.1.97 via nvm.
Unable to replicate. I'm on a Mac (OS 10.6.7) running bash and Ruby 1.9.2p180. I don't get your tilt warning, either.
I also haven't experienced this problem when using The Middleman (Sinatra-based) or Rails 3.1; both use the same coffee-script gem (as does Tilt; I suspect the coffee_script is just a typo).
Have you tried updating all the pertinent gems (sinatra, coffee-script, tilt, execjs) to their latest versions? What JS environment do you have on your system (e.g. do you have node, or are you relying on therubyracer)?
On Ruby 1.9.2, Sinatra 1.3.2, CoffeeScript 2.2.0 and Node 0.6.2 this does not occur anymore.

Ruby Twitter gem

-- UPDATE --
Ok its fixed. This is what I did. remove all ruby and rubygems completely. then install ruby1.9.1-full and rubygems1.9.1 then install the twitter gem.
Hi guys,
I am having trouble working with the Twitter gem. I am using ruby 1.8.7
After installing when I try to run a simple script I get this error
ruby twitter.rb
./twitter.rb:5: uninitialized constant Twitter (NameError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:inrequire'
from twitter.rb:2
I running this on a Ubuntu box. I checked with gem -list and I see the Twitter (1.1.0) is listed there.
this is the code I am trying to run
require "rubygems"
require 'twitter'
puts Twitter.user_timeline("test").first.text
Any ideas ?
I believe it only works with Ruby 1.9 If you want to use twitter gem try version 0.9 with Ruby 1.8.x
This works for me:
ruby-1.9.2-p0 > require 'twitter'
=> true
ruby-1.9.2-p0 > puts Twitter.user_timeline("test").first.text
TExES Pedagogy and Professional Responsibilities EC-12 Teacher Certification Test Prep Study Guid… - by Sharon A Wynne http://amzn.to/f3kF74
=> nil
which version of ruby are you using?
gouravtiwari21's comment seems to fix the problem, but it's wrong to suggest that the twitter gem requires 0.9.0 if you want to run it using Ruby 1.8.x.
You can check out the version compatibility here:
http://travis-ci.org/#!/jnunemaker/twitter
It shows the twitter gem working with Ruby installs as low as 1.8.7.
For me, it was an issue with having the correct dependent gems, as well as the correct versions.
Here's how I got it working:
I ran:
sudo gem list
And compared the versions of specific gems with what I found here:
https://github.com/jnunemaker/twitter/blob/master/HISTORY.md (I simply searched for the word 'dependency' to see which versions twitter cared about.
I also found this diff:
https://github.com/jnunemaker/twitter/commit/ac8114c1f6ba2da20c2267d3133252c2ffc6b6a3
And I compared the gems listed there with what I had installed, and I just made sure my system lined up with what I was seeing in the version notes. Oftentimes what happened is that I had multiple versions of a gem, and for some reason, the lower version was taking precedence.
I'm not sure why I still have to add
gem 'twitter', '1.7.1'
to my Gemfile, but alas, that's the last step required in order to get this stuff working.
Don't forget to restart your server, and you should be good!

Resources