Rspreadsheet gem - ruby

I want to use the rspreadsheet gem, first I tried with bundler but each time there is this error: `require ': cannot load such file - rspreadsheet.
So I installed the gem directly and when I run my program to test:
workbook = Rspreadsheet.open('./print.ods')
sheet = workbook.worksheet(1)
It tells me: uninitialized constant Rspreadsheet (NameError).

Assuming your script is named script.rb and you have already run bundle install could you try running bundle exec ruby script.rb to see if that works?

Related

"Could not find rake in any of the sources" when calling ruby script from a GEM

I have a GEM that I build from a project I have, let's call it MyGEM. In the bin directory of MyGEM, I have a MyShellScript.sh that calls rvm to switch to JRuby and runs a script using JRuby that also exists in MYGEM but in another directory, this script is called MyRubyScript.rb
I added MyGEM to another ruby project that I am using called MyProject. Now in MyProject, after installing MyGEM, I called bundle exec MyShellScript.sh. The commands ran fine, but when MyShellScript.sh calls MyRubyScript.sh, I get the following error:
Could not find rake-10.4.2 in any of the sources
I tried to call bundle install rake, bundle update and gem install rake and I still get the same error. Also it seems that I get this error with any ruby command, even if it is not related to rake. So, for example, I get it when I call gem list in MyShellScript.sh and also I get it when I clear MyRubyScript.rb and put only puts "Hello" in it and then call bundle exec MyShellScript.sh again from MyProject.
Also, when I go to the MyGEM location from MyProject using cd $(bundle show MyGEM)/bin and then call the script using ./MyShellScript.sh, it works fine and I do not get the rake error.
I ran into the same issue in OS X and the following fixed it for me
brew install llvm
ruby-install rbx 2.3.0 -i ~/.rubies/rbx -- --llvm-config /usr/local/opt/llvm/bin/llvm-config

Gmail gem error when run inside Rspec - 'cannot load such file -- gmail/client'

I'm using this gem called gmail and it works perfectly when run in just a plain ruby script. However, when used inside Rspec, I keep getting this error.
Failure/Error: #gmailnew = Gmail.new()
LoadError:
cannot load such file -- gmail/client
# /Library/Ruby/Gems/2.0.0/gems/gmail-0.6.0/lib/gmail.rb:50:in new'
# ./temp.rb:7:inblock (2 levels) in '
Any idea what causes this?
- I've already tried reinstalling it since it seems to be not finding a file.
I could not figure out what causes the conflict, similar problems by others.
Why is autoload failing to load files for gems
Ruby autoload conflicts between gmail and parse_resource gems
I was able to resolve this by using Bundler.
1) Added gem inside the Gemfile (gem 'gmail')
2) $ bundle install - this installs all gems inside the Gemfile, and avoids the conflicts
3) $ bundle exec rspec my_spec_file.rb - executes Rspec

Run gem from local source code

I am currently working on developing a gem and I would like to be able to run that gem from command line and point it to my local source. Previously I've done this in rails by specifying the path in the gem file but now I would like to run the gem on a non rails app. So I would like to know how to call the gem from command line and specify that I want to use the source code in a certain directory.
For example can I cd into the directory that I want to run it on and do something like: ruby my_gem --path=~/code/mygem
Also do I have to build them gem or can I run it from source without building it?
From the root directory of your gem, try this to execute lib/MyGem.rb:
ruby -Ilib lib/MyGem.rb
or test your gem interactive:
irb -Ilib
> require 'mygem'
true

How to reference a local gem from a ruby script?

I need to reference a local gem from a plain ruby script, without installing the gem. On the trail of How to refer a local gem in ruby?, i tried creating a Gemfile with the following setup:
%w(
custom_gem
another_custom_gem
).each do |dependency|
gem dependency, :path => File.expand_path("../../#{dependency}", __FILE__)
end
and the script looks like this:
require 'custom_gem'
CustomGem::Do.something
When I execute this with:
bundle exec ruby script.rb
I get:
script.rb:1:in `require': cannot load such file -- custom_gem (LoadError) from script.rb:1:in `<main>'
If I leave out the require 'custom_gem' , I get:
script.rb:3:in `<main>': uninitialized constant CustomGem (NameError)
I even tried without bundler, and just writing gem ... :path =>̣ ... in the script itself, but without results. Is there any other way of referencing custom gems from ruby scripts, without installing the gems locally?
Make sure that your gem name as same as in Gemfile (e.g. custom_gem)
# Gemfile
source "https://rubygems.org"
gem "custom_gem", path: "/home/username/path/to/custom_gem"
Don't forget to actually install this gem using bundler
bundle install
After that, the script should be ready to use by bundle exec ruby script.rb
# script.rb
require 'custom_gem'
CustomGem::Do.something
Without using a Gemfile, you can install a local version of a gem by running bundle exec rake install in the gem's root directory and then you can reference it just like any other installed gem.

Why do I need bundle exec to require this rubygem?

I have a Rails app with some non-Rails-dependent files under `lib/services'. One of these files uses the Domainatrix gem.
require "domainatrix"
class SuggestionParser
# various suggestion parsing methods
end
I have an empty spec for this file under spec/lib.
require "services/suggestion_parser"
describe SuggestionParser do
end
Unfortunately, when I try to run that spec without bundle exec I hit an error:
$: rspec spec/lib/services/suggestion_parser_spec.rb
-> /Users/davidtuite/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require': cannot load such file -- domainatrix (LoadError)
Every other spec and gem in my project will run without using bundle exec. Why do I need to prefix this one in order to get it to run?
For convenience, here's a link to the Domainatrix gemspec.
My guess would be that domainatrix is declared using the :path or :git options in the Gemfile, neither of which install the gem in a way that makes it accessible to rubygems.
This could be confirmed if you post the line for domainatrix from the Gemfile.
try running the following commands:
$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ bundle install --without production --binstubs=./bundler_stubs
This won't solve the specific problem with your gem, but it will take away the necessity to type in bundle exec every time you run your tests if you're using rvm.

Resources