How can I find out the version of a library that is packaged with Ruby, e.g. json? - json

Some Ruby functionality packaged with the Ruby distribution (and not needing to be installed as gems explicitly or via bundler). JSON is one example (require 'json'). It can be required in Ruby code but doesn't require gem installation.
Yet JSON is a gem on Github, at https://github.com/flori/json.
So how can I know which version of that gem I'm getting when I require 'json' in my code?

Alternatively many ruby gems specify their version in a constant.
You can utilize this to determine the version you are using in code or in console as well e.g.
require 'json'
JSON.constants.grep(/VERSION/)
#=>[:VERSION, :VERSION_ARRAY, :VERSION_MAJOR, :VERSION_MINOR, :VERSION_BUILD]
JSON::VERSION
#=> 2.5.1
require 'bundler'
Bundler.constants.grep(/VERSION/)
#=> [:VERSION]
Bundler::VERSION
#=> 2.2.3

You can find the Ruby home directory, and search below it for a directory of the appropriate name, and then inspect its version.rb file. For example, with rvm-managed rubies, I can do this (on a Posix-complaint system such as Linux or Mac OS):
$ cd $(which ruby)/../..; pwd
/Users/keith.bennett/.rvm/rubies/ruby-3.0.1
$find . -type d -name '*json*'
./lib/ruby/3.0.0/psych/json
./lib/ruby/3.0.0/json
./lib/ruby/3.0.0/rdoc/generator/template/json_index
./lib/ruby/3.0.0/x86_64-darwin19/json
./lib/ruby/gems/3.0.0/gems/json-2.5.1
./lib/ruby/gems/3.0.0/gems/rbs-1.0.4/stdlib/json
I can cat ./lib/ruby/3.0.0/json/version.rb | grep 'VERSION ' (include the space after "VERSION"), and I get:
VERSION = '2.5.1'
..which is also the version of the gem in the gems directory included in the listing above.
So I can see that 2.5.1 is my JSON version.

Related

Is using -S with JRuby recommended even when using a Ruby version manager?

Based on this page on the JRuby wiki and other resources I see the recommendation is
to always use jruby -S.
because
The -S parameter tells JRuby to use its version of the installed binary, as opposed to some other version (such as an MRI version) that might be on your PATH.
Using rbenv and rvm I've had issues when using -S with JRuby (see this question) and my understanding is that both those version managers solve the problem -S attempts to address.
You do not need to run the gem command with jruby -S gem if you are using a Ruby manager such as rbenv or rvm. Those tools manage your command path for you so that the correct Ruby runtime and the correct version of rubygems are used. Similar applies to the rake.

Error installing "nokogiri" in a Ruby on Rails application?

I've been following along with the Lynda.com's Ruby on Rails course. I did everything just as mentioned inside the videos.
I am trying to run the rails server command, which should default to WEBrick, correct?
I run the command and it has an issue in the nokogiri.rb file and on line 29 where the error is happening this is what is read:
require 'nokogiri/nokogiri'
Which is what my command prompt is throwing up on when running the rails server command. Any idea what could be causing this? If so, what commands do I need to run to settle this? To me it sounds like something was missed when installing MySQL.
I am not sure what nokogiri is and why rails server won't run and points to that line of code.
The path to this file is (Wherever you installed the folder to)\lib\ruby\gems\2.2.0\gems\nokogiri-1.6.6.2-x64-mingw32\lib
EDIT**: I have a chain of errors, like it goes from -long path-, from -longpath- etcetera. I tried installing the latest gem, and it wasn't successful. Is there a log file I can get to share with you guys to help figure out the issue.
nokogiri is a Ruby "gem" (library) for parsing XML and HTML.
Your Rails project should have a Gemfile which specifies the name and version of each gem needed to run the project. To install the gems listed in the gemfile:
bundle install
After that install completes successfully, Rails should be able to find the nokogiri gem.
Try this
$ sudo apt-get install libxslt-dev libxml2-dev zlib1g-dev
$ sudo gem install nokogiri -v '1.5.11'
According to \lib\ruby\gems\2.2.0\gems\nokogiri-1.6.6.2-x64-mingw32\lib, which means that you are running it on Windows platform. You are using ruby 2.2.0.
I know that nokogiri conflicts with 2.2.* versions of ruby in Windows. I have already encountered this problem. I think it is because of mingw32.
You need to install 2.1.* version of Ruby in order to make Nokogiri work.

How to set correct value for JRUBY_HOME if I am using rbenv

I installed JRuby with rbenv
ruby -v
jruby 1.6.7.2 (ruby-1.9.2-p312) (2012-05-01 26e08ba) (Java HotSpot(TM) 64-Bit Server VM 1.7.0_01) [linux-amd64-java]
which ruby
~/.rbenv/shims/ruby
gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.24
- RUBY VERSION: 1.9.2 (2012-05-01 patchlevel 312) [java]
- INSTALLATION DIRECTORY: /home/myjava_api/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /home/myjava_api/.rbenv/versions/jruby-1.6.7.2/bin/jruby
- EXECUTABLE DIRECTORY: /home/myjava_api/.rbenv/versions/jruby-1.6.7.2/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-java-1.7
- GEM PATHS:
- /home/myjava_api/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8
- /home/myjava_api/.gem/jruby/1.9
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "install" => "--no-rdoc --no-ri"
- "update" => "--no-rdoc --no-ri"
- REMOTE SOURCES:
- http://rubygems.org/
But $JRUBY_HOME is not set
When I set $JRUBY_HOME to "/home/myjava_api/.rbenv/versions/jruby-1.6.7.2/bin/jruby" and after re-login when I executed "ruby -v" I got error: "Error: Could not find or load main class org.jruby.Main"
When I remove $JRUBY_HOME variable "ruby -v" works fine
What must be correct $JRUBY_HOME value?
I was trying out this tool (rbenv) after managing my PATH by hand for a while, for the first time and got this same error: Error: Could not find or load main class org.jruby.Main
I had completely forgotten to unset the old JRUBY_HOME setting in my .bash_rc file, after uninstalling my previous version of jruby to try out rbenv.
In response to the other answers, I have certain tools and libraries that require the JRUBY_HOME variable to be set in order to work, http://vertx.io/ is one example. On the other hand, a number of gems and tools these days go the other route, and seem to assume that you are using either rvm or rbenv. So using one of those tools, but continuing to set JRUBY_HOME seems to be the path of least resistance.
Fortunately, when JRUBY_HOME is set correctly, it will work just fine with rbenv. For instance, mine is set like this export JRUBY_HOME="/home/user/.rbenv/versions/jruby-1.7.1"
The original poster's error appears to be trying to set the variable to the jruby executable, when in fact JRUBY_HOME should simply be the entire jruby directory. I think his should be "/home/myjava_api/.rbenv/versions/jruby-1.6.7.2". Hope that clears things up. I realize this is an old question, but when I searched the error, it was the first relevant result.
The whole point of utilities like rbenv or rvm is to be able to switch between ruby implementations without worrying about changing environment variables, as they do it for you; you shouldn’t need to set $JRUBY_HOMEto run JRuby (which incidentally for you would be /home/myjava_api/.rbenv/versions/jruby-1.6.7.2) as these utilities are aimed to make the underlying implementation transparent.
I guess the obvious question is why do you want $RUBY_HOME if it works fine without it?
JRuby is so simple to install you don't need any version manager. Just download the Zip file and extract it to any directory you like. Then add the JRuby/bin directory to your path. In your case that would seem to be /home/myjava_api/.rbenv/versions/jruby-1.6.7.2/bin.
If you have several projects that require different versions of JRuby or different groups of gems just extract a copy of the appropriate JRuby version for each project. Assuming you use the correct PATH for each project the correct version of JRuby will run and it will have all the gems you installed for that project, and each project will be quite separate.

issue with JRuby + OpenURI + HTTPS

I have problem with very simple program I'm trying to write using JRuby, Open URI and JRuby-OSSL.
I installed JRuby-OSSL gem using:
gem install jruby-openssl
and downloaded latest jruby-complete jar (1.6.7).
I have simple script test.rb:
require 'jruby/openssl/gem_only'
require 'open-uri'
open('https://google.com')
and when I run it like this:
java -jar jruby-complete-1.6.7.jar test.rb
I get:
NameError: uninitialized constant Net::HTTP::OpenSSL
const_missing at org/jruby/RubyModule.java:2642
use_ssl= at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/net/https.rb:124
open_http at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:231
buffer_open at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:616
open_loop at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:164
catch at org/jruby/RubyKernel.java:1183
open_loop at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:162
open_uri at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:132
open at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:518
open at file:/Users/mislocin/.m2/repository/org/jruby/jruby-complete/1.6.7/jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/open-uri.rb:30
(root) at test.rb:4
I'm pretty sure I'm missing some details, any help appreciated.
try requiring openssl instead :
require 'openssl'
require 'open-uri'
open('https://google.com')
but than of course this probably won't solve you problem.
you gem install jruby-openssl among your jruby gems, but than do not use jruby but invoke java directly - you should not rely on it resolving gems based on your ENV variables.
you also should expect a SSLError as you haven't configured it to ignore certificate verification, here's a sample how to get https:// URIs: https://gist.github.com/1361989
OK, found my error.
By mistake, I configured GEM_HOME variable pointing to my gem repo instead of GEM_PATH. After configuring GEM_PATH environment variable everything works fine now.

Problem with Rails 3 and MySQL related to JSON dependency in ActiveSupport

I built my Rails 3 app using sqlite and now I'm trying to switch over to MySQL.
I created a new MySQL db, changed database.yml accordingly, and I added an older version of the mysql2 gem to my gemfile (gem 'mysql2', '< 0.3') which is supposed to play nicer with Rails 3.
I can start the dev server fine. When I visit a page, I get a Mysql2::Error (Table 'twitter_quiz_development.users' doesn't exist). Which is fine--I still need to add the schema to the new db. This is where I run into trouble:
rake db:schema:load returns this nasty error.
The solution found here did not work for me. gem pristine --all return this.
I'm running Ubuntu 11 if that helps.
It appears that the error has to do with JSON but I have no idea how to solve this. Thanks.
The issue(s) you are experiencing could be due to 3 things:
The version of Ruby you are using
The use of the SystemTimer gem with Ruby 1.9
The JSON gem
I've elaborated below...
The first thing I would suggest is to upgrade your version of Ruby 1.9. The latest stable is:
ruby 1.9.2p180 (2011-02-18 revision 30909)
I would highly recommend using Ruby Version Manager (RVM) to install and manage Rubies.
In terms of using the JSON gem, have a look at:
http://ruby.about.com/od/tasks/a/The-Json-Gem.htm
To quote:
On Ruby 1.8.7, you'll need to install a gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution. So, if you're using 1.9.2, you're probably all set. If you're on 1.8.7, you'll need to install a gem.
Before you install the JSON gem, first realize that this gem is distrubuted in two variants. Simply installing this gem with gem install json will install the C extension variant. This requires a C compiler to install, and may not be available or appropriate on all systems. Though if you can install this version, you should.
If you can't install the C extension version, you should gem install json_pure instead. This is the same gem implemented in pure Ruby. It should run everywhere that Ruby code runs, on all platforms and on a variety of interpreters. However, it's considerably slower than the C extension version.
Edit:
Looks like you should avoid using the SystemTimer gem with Ruby 1.9. See this for more info: http://isitruby19.com/systemtimer
To quote a comment made on that page by Phillipe:
I am the (co)-author of this gem. Short story : Using this gem in Ruby 1.9 is useless and does not make any sense!
System Timer is trying to work around some limitation of the "green thread" model used in Ruby 1.8 (MRI). See http://ph7spot.com/musings/system-timer for more details.
It is then irrelevant in a Ruby 1.9 which abandoned the green thread model and embraced native threads (kind of since there is still a global interpreter lock).
Cheers,
- Philippe
Your gem pristine --all looks like it errored out with the SystemTimer gem before it got to the json gem. Is that gem used in the Rails app in question?
Are you using RVM? If so, I'd manually remove the SystemTimer and json gems and their binaries (if any) from ~/.rvm/gems/ruby-whatever-version/ (in the gem and bin subdirectories).
Try to update to the latest version of the JSON gem (1.5.1) by adding this to your Gemfile:
gem "json", "1.5.1"
And then run bundle install to install SystemTimer and the later version of the json gem.