How to set specified gem version for Ruby app? - ruby

I`ve encountered the problem after updating some gems, so basically all older gems are still available but i cant force application use them.
Lets say, i need something like that:
require 'rubygems'
require 'mygem', '0.1.2'
Is there a way to do it?

Well, always happens to me. Found an answer.
require 'rubygems'
gem 'mygem', '=0.1.2'

require 'rubygems'
require 'activerecord', '=1.15.3'
This worked for me.
Check this out. Seemed helpful
Link

Related

Kaminari pager not working with Sinatra and Mongoid?

Can't get Kaminari to work with Sinatra and Mongoid. I'm getting this error:
NoMethodError at /api/events
undefined method `page' for #<Mongoid::Criteria:0x007fccb7828c38>
Here is minimal code to get the error:
Gemfile
source "https://rubygems.org"
gem 'mongoid'
gem 'sinatra'
gem 'kaminari-mongoid'
gem 'kaminari-sinatra'
server.rb
require 'mongoid'
require 'sinatra'
class Event
include Mongoid::Document
end
get '/events' do
Event.desc(:id).page(params[:page]).per(10)
end
I have tried require 'kaminari', require 'kaminari-sinatra', require 'kaminari-mongoid', all to no avail (I get LoadErrors). I've also tried register Kaminari::Helpers::SinatraHelpers as mentioned here, which also failed.
I've followed the instructions in detail, and have scoured Google and StackOverflow to no avail. This answer didn't work. I can't help thinking I'm missing something easy; I'm not a Ruby veteran. My hunch is it's something with Bundler. Any idea?
I ran into the problem as well. Unfortunately, kaminari-mongoid has a rails dependency (you can look in the gemspec file here: https://github.com/kaminari/kaminari-mongoid/blob/master/kaminari-mongoid.gemspec). Therefore, it is not possible to use both kaminari-sinatra and kaminari-mongoid.
This solved my problem. https://github.com/ajsharp/mongoid-pagination. Add it to your Gemfile and install with Bundler.
In your app.rb file, require 'mongoid-pagination'

Ruby , Gems -Getting started

I have just started learning ruby . There are few things I am quite confuse with in ruby as I used to work with Perl and C.
1) How to add external library like "Mechanize" to use with your script?
Ruby has virtually standardized on using bundler to manage dependencies. For any project you create a Gemfile that looks roughly like:
source 'https://rubygems.org/'
gem 'mechanize'
Then you would run bundle install to be sure your gems are loaded correctly.
Inside your application you'd have:
require 'rubygems'
require 'bundler/setup'
require 'mechanize'
# ...
If you want to build your own gem, the best thing to do is read the documentation and look at the source of other gems to see how they do it. Every gem has to follow certain conventions to work correctly, but these are pretty obvious if you look at more than a few of them.
You can even use bundler to help build a new gem which can simplify the process considerably.

After installing rbenv I need to add require 'rubygems'

I replaced rvm with rbenv on my Macbook and after doing so I've needed to add require 'rubygems' to things to get them working.
Is there a way of forcing rubygems to be required, or removing the need for it?
For sanity reasons you should always require what you need. As rubygems is included by default in ruby 1.9 you should do something like:
require 'rubygems' unless defined?(Gem)
at the top of your script.

Require a gem library

I'm kinda new to ruby and I'd like to use a gem charting library, but for some reason when I require it in the ruby script on my desktop it doesn't work. However when I require in my irb it does work. Is there a way to fix this?
Try using bundler if you can and declare the requirements in a Gemfile. This will make your environment much more consistent between different computers and will provide a reference of the dependencies you have.
The Bundler setup procedure is pretty simple and well documented. It will load in all the gems and any of their dependencies automatically.
Generally the problem with the require statement failing is the library is not in your $LOAD_PATH, and that's usually because you haven't loaded rubygems:
require 'rubygems'
gem 'somegem'
require 'somegem'

require 'rubygems'

I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/). What is purpose of this require?
# require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello world!"
end
In all cases the code works without this line.
require 'rubygems' will adjust the Ruby loadpath allowing you to successfully require the gems you installed through rubygems, without getting a LoadError: no such file to load -- sinatra.
From the rubygems-1.3.6 documentation:
When RubyGems is required, Kernel#require is replaced with our own
which is capable of loading gems on demand.
When you call require 'x', this is what happens:
If the file can be loaded from the existing Ruby loadpath, it
is.
Otherwise, installed gems are searched for a file that
matches. If it's found in gem 'y', that gem is activated
(added to the loadpath).
The normal require functionality of returning false if that file
has already been loaded is preserved.
See the documentation for Kernel#require to understand why this is necessary.
It is often superfluous. It will allow you to require specific versions of particular gems though, with the gem command.
https://guides.rubygems.org/patterns/#requiring-rubygems
As an addition to prior (and correct answers): Ruby 1.9 and newer ship with RubyGems built-in, so there is no real need to require 'rubygems'. Source here

Resources