Programatic way to retrieve the Ruby version specified in the Gemfile? - ruby-on-rails

I am looking for an efficient a programatic way to retrieve the Ruby version specified in a Rails app Gemfile.
# Gemfile
ruby '2.3.4'

You can use Gem.ruby_version
Gem.ruby_version.to_s
=> "2.2.5.319"

Related

vestal_versions GEM error -Cannot define multiple 'included' blocks?

I am trying to use the GEM "vestal_versions" for version control in my Ruby on Rails application,
ruby 2.1.2p95 (2014-05-08 revision 45877)and Rails 4.1.1 with postgresql as back end.I followed the steps mentioned in
enter link description here
but am getting some error while doing data base operations.ActiveSupport::Concern::MultipleIncludedBlocks: Cannot define multiple 'included' blocks for a Concern
.how to rectify this error?
That repository needs updating. The pull request is here with the fix. You can find a working version at https://github.com/altagem/vestal_versions/tree/rails-4_1
Add this line of code to your gemfile and you should be good to go.
gem 'vestal_versions', :github => 'altagem/vestal_versions', branch: 'rails-4_1'

In Rails, why sqlite works without config.gem 'sqlite3-ruby'

With a new Rails 2.3.10 project, the file config/environment.rb has the following line commented out:
# config.gem "sqlite3-ruby", :lib => "sqlite3"
but for some reason, I tried a scaffold foo, and start the rails server, and the app is running.
I thought the requirement is, every gem the app needs, it has to be listed in config/environment.rb?
In Rails 2.3, it's enough to have the gem installed on your system for you to use it.
In Rails 3, you must have the gem listed in your Gemfile and installed via bundler to use the gem.

conditionals in Gemfile

Our team uses different databases for each other, and we are using bundler so our Gemfile contains the repo creator's db connector(mysql)
I am using pg and due to a bit laziness and fear of breaking something, I don't want to use mysql, so I just add a gem "pg" in our Gemfile.
Of course, since we're using git, it will always show as a modified file, and we all use the Gemfile so we can't gitignore it or commit it with our changes.
Question is, how do we go about this? Is there a conditional in bundler or do I just have to declare that I'm using a certain gem someplace else?
Since Gemfile, like Rakefile, is just a chunk of Ruby, you can throw in conditionals if you think it will simplify your life. For instance:
if (Gem.available?('pg'))
gem 'pg'
else
gem 'mysql2'
end
Sometimes you have to do this for different Ruby versions as 1.8 and 1.9 sometimes need different gems.
You can use a group. Yehuda Katz explain it how here (taking in example the pg gem) http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/

Does a Rails 3.0.0.beta app only see bundled gems in the console?

I have a library that I'm trying to get working with rails 3 (specifically feedzirra) which I can require ok in irb but it breaks the console in my app with the following error:
http://pastie.org/855976
Found a bit of info on using feedzirra with rails 3. It looks like your problem might be with the Loofah library feedzira uses. It uses the deprecated config.framework.
Here's a link with some more info http://www.mythoughtpot.com/2010/02/10/feedzirra-on-rails3/
Rails3 modifies the $LOAD_PATH so it only contains gems listed in the Gemfile.
($LOAD_PATH is an array of directories where Ruby searches for libraries).
So you must add the Gem to the Gemfile and run bundle install.
You can check if the gem is in your path by typing puts $LOAD_PATH.grep(/feedzirra/) in the rails console.
For more information on using Bundler in Rails3 check out these:
http://railsdispatch.com/posts/bundler
http://railscasts.com/episodes/201-bundler

Why does Rails 2.0.2 Load?

Why does this happen?
ruby script/console
Loading development environment (Rails 2.0.2)
>> exit
jay-z-mac-pro:justinz$ rails -v
Rails 2.3.3
Look at your RAILS_ROOT/config/environment.rb file. In it you can set and lock the Rails gem version to load. Look for the line:
RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION
Change that to
RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION
Maybe your project is pinned to version 2.0.2. I haven't worked with Rails for quite some time, so I can't say for sure, but I believe this can be done in a config file.
Also, you should check if Rails is in your vendor directory (was that what it is called?). That's another way to force a specific version.
Check your config/environment.rb, it sets the Rails version for your application. It should be something like this:
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
script/console will load your application with the gems (including Rails) that you define in the config. rails is just the latest rails gem installed.
However, moving from Rails 2.0.2 to 2.3.3 is a pretty big change, you'll want to be careful about upgrading. Make sure to read the docs as it relates to your application.

Resources