Ruby 1.9.2 is backward compatible with 1.8.7? - ruby

I'm using Ruby 1.9.2 (Yarv).
When I install gems I just use "gem install ".
How can I know which Ruby version they are written in: 1.8.7, 1.9.1 or 1.9.2?
If I'm using the latest version and I install a gem that is written in 1.8.7, then that gem doesn't work?
Eg. Yardstick gem seems to not work with 1.9.2 but perfectly with 1.8.7.
How do you check what version each gem is using?
Could someone enlighten this topic for me.

Unfortunatelly there is no definitive way to check that as there are so many versions and variants or Ruby interpreters (Ruby, JRuby, Iron Ruby) and system platforms. Also one gem which is pure Ruby may not work on all platforms with same interpereter verson, because some OS specific properties could be used.
So you must trust the autor or docs he/she provided. Another way to make sure that your platform is right for gem just run tests enclosed to gem - every well written gem should have such.
There is also small tip: if file contains
# -*- encoding: utf-8 -*-
then autor probably developed the gem with 1.9.x in mind.

There's a website for answering the question: "Is it Ruby 1.9?"
http://isitruby19.com/
It doesn't look like anyone has reported on Yardstick yet:
http://isitruby19.com/yardstick
You could be the first, and report that it's broken in 1.9.

Related

How to control which version of Ruby gets used when running bundle

In ~/src/project/ containing all the files, I'm attempting to run bundle and bundle install, but it tells me I need a gem called "buff-extensions" and that gem installer requires Ruby version >= 2.2.0 to work properly.
When I ran ruby --version, it said Ruby 2.2.4p230 is being used when run from that directory.
I know there are several versions of Ruby on the machine in several places, and I'm struggling to follow the paths and figure out which technology is using which piece.
The Bundler docs spell this out:
You can specify the required version of Ruby in the Gemfile with ruby. If the Gemfile is loaded on a different Ruby version, Bundler will raise an exception with an explanation.
ruby '1.9.3'
What this means is that this app has a dependency to a Ruby VM that is ABI compatible with 1.9.3. If the version check does not match, Bundler will raise an exception. This will ensure the running code matches. You can be more specific with the :engine and :engine_version options.
ruby '1.9.3', :engine => 'jruby', :engine_version => '1.6.7'
I finally figured out the exact incantation in order to get the intended result, and I now realize the situation is unreasonable to expect any stranger to attempt to answer.
It involved chef, and I was supposed to run chef exec bundle.

IMDB gem not working on Windows with Ruby 2.2 - Can't use Nokogiri 1.6.7.rc4

I am trying to use the IMDB gem on Windows with Ruby 2.2 but this gem seems to force using an older version of Nokogiri which is not compatible with my Ruby's version on Windows.
This is the message I am getting:
The bundle currently has Nokogiri locked at 1.6.2.1.
I do have installed the RC4 version.
Is there any way to force IMDB to use the RC4 version?
Or other solution I did not think about?
Probably the easiest solution on Windows is to create a Virtual Machine with Linux and then install Ruby.
Windows has many problems with properly handling gems.
Preamble and warning
This solution is only an ugly hack and I don't recommend to use it.
The best solution would be a corrected version of imdb-gem with a (possible) “optimistic” version constraint.
Disclaimer 2: I don't use bundler, maybe this solution does not work exactly as I show it. But the process should be similar for bundler.
Source of the problem
The gemspec of the imdb-gem contains the command:
s.add_dependency 'nokogiri', '= 1.6.5'
or in version 0.8.2
s.add_dependency 'nokogiri', '= 1.6.2.1'
If this would contain an optimistic version constraint like
s.add_dependency 'nokogiri', '=~ 1.6'
you could use the version you use (assumed there is no reason for this specific version).
Hacking the gemspec
During gem install the gemspec is copied to a location like
[ruby-installation-path]/lib\ruby\gems\2.1.0\specifications.
Look for the file imdb-0.8.2.gemspec and make this change:
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
# Begin of Hack the nokogiri version
# s.add_runtime_dependency(%q<nokogiri>, ["= 1.6.2.1"])
s.add_runtime_dependency(%q<nokogiri>, ["~> 1.6"])
# End of Hack the nokogiri version
s.add_development_dependency(%q<rake>, ["~> 10.0.3"])
Then try if your script works.
I tested with nokogiri version 1.6.6.2 and detected no problem.
If the version 1.6.7.rc4 works also I would recommend to ask for a change on https://github.com/ariejan/imdb and a new version of the imdb-gem.

how to set a ruby application to run on multiple environments

I have developed a simple ruby application without any MVC, and runs on command prompt using ruby 1.9.2 . It also has a Gemfile. I want this application to run on machines running ruby 1.8.7 and 1.9.3 as well. So how can I set it right for multiple environments.
Please Guide.
Thanks
At first you have to make sure that your code is compatible with the syntax of ruby 1.8 and 1.9 https://stackoverflow.com/a/21621/335523
Then make sure that the gems you used support both versions of ruby. (Read through their github page or documentation)
If you are using gems that do not suport both versions of ruby you have to find alternative gems that provide the same functionality in each environment and specify them in your gem file like this:
gem 'rcov', :platforms => :ruby_18
gem 'simplecov', :platforms => :ruby_19

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!

How do I add conditional rubygem requirements to a gem specification?

Is it possible to add a gem dependency only if the person is using a certain version of ruby?
Background: I'm working on a fork of a project that uses Test::Unit::Autorunner and the like. They are part of the standard library in ruby 1.8, but aren't part of the standard library in 1.9.1, and is instead in the "test-unit" gem. I want to add a dependency that says that if someone's using ruby 1.9.1 or later, install the "test-unit" gem, but if they're using 1.8 or earlier, they don't need to install anything.
If you look at the gemspec documentation for add_dependency, there isn't an option for a ruby version. Perhaps you could use the post_install_message attribute to tell the user to install the gem if they're using ruby 1.9.
I did this exact thing for a project. The trick is to add the script as an extension, which will then get executed at install time on the user's machine.
Here are code snippets and links to our github:
First, when in the gemspec (we're actually using a Rakefile to generate it, but the result ends up the same) source
# This file *needs* to be named this, there are other files
# for other extension processors
s.extensions << 'ext/mkrf_conf.rb'
And then the relevant lines in that mkrf_conf.rb source
require 'rubygems/dependency_installer.rb'
inst = Gem::DependencyInstaller.new
inst.install "test-unit" if RUBY_VERSION > "1.9"
Gem doesn't support conditional dependencies (except on gem builder's environment -as noted above), and bundler is not a viable option to solve this either - see https://github.com/carlhuda/bundler/issues/1281
hay ... i'm kind of a ruby newbie ... if this is the best way to do it.
any way ... i wish i can do that using only Ruby .... though u can use your operating system shell to do that, by using this in your program installer, just execute (works for Linux based operating systems):
$ruby --version
(u can execute that from a ruby installer file, just like: ruby --version)
and put a possibility according to output, if it's 1.9.1 add an extra line to execute:
$ sudo gem install gem_name
else, just leave it be.
Checkout this tutorial in the Ruby Programming wikibook.
Tt shows how to install different versions of dependencies depending on what version of ruby the installee is using.
(short answer--it ain't as easy as it should be)
You can't. You need to build two gems, one with
spec.required_ruby_version = '~> 1.8.6'
and one with
spec.required_ruby_version = '~> 1.9.1'
spec.add_dependency 'test-unit', '~> 2.0.5'
Gemspecs are just ruby files anyway, so you can execute any ruby code inside them, so:
spec.add_dependency = 'test-unit', '>= 2.0' if RUBY_VERSION =~ '1.9'
EDIT: Specs run only on the builders machine.

Resources