Unable to open excel with Roo or Spreadsheet Gems - ruby

When I try this with roo gem:
irb(main):001:0> require 'roo'
=> true
irb(main):002:0> oo = Excel.new("C:/Users/Abash/Desktop/test1.xls")
**NameError: uninitialized constant Excel** from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
When I try this with spreadsheet gem:
irb(main):001:0> require 'spreadsheet'
=> true
irb(main):002:0>Spreadsheet.client_encoding = 'UTF-8'
=> "UTF-8"
irb(main):003:0> book = Spreadsheet.open 'C:/Users/Abash/Desktop/test1.xls'
**Errno::EACCES: Permission denied** - C:/Users/Abash/Desktop/test1.xls
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
Can somebody show me a work around for these errors?

I believe you are using ruby 1.9 or above version. In such a case,you need to specify the gem while creating a new instance.
require 'roo'
s =Roo::Excel.new("myspreadsheet.xls")
s =Roo::Excelx.new("myspreadsheet.xlsx")

Related

Ruby: End of file reached with gem serialport

Using gem serialport in ruby i'm having the error code EOFError: end of file reached
irb
2.0.0-p451 :001 > require 'serialport'
=> true
2.0.0-p451 :002 > serial_port = SerialPort.new "/dev/ttyUSB0", 115200
=> #<SerialPort:fd 7>
2.0.0-p451 :009 > serial_port.write "#00RD0000020054*\r"
=> 17
2.0.0-p451 :010 > r = serial_port.readline("\r")
EOFError: end of file reached
from (irb):10:in `readline'
from (irb):10
from /home/user/.rvm/rubies/ruby-2.0.0-p451/bin/irb:12:in `<main>'
any suggestion?
Try crate separate SerialPort objects for reading and writing. It seems to be an issue with Ruby - see here: http://www.rngtng.com/2009/11/27/if-your-ruby-serial-port-doesnt-read-what-youre-sending/
Attempt the following
serial_port.readlines
Instead of the
serial_port.readline("\r")
I might also suggest
serial_port.readlines("\r")

"NameError: uninitialized constant UserAgent" when using Watir

I'm new to ruby, I seem to have successfully installed watir-webdriver and webdriver-user-agent gems, but when I was trying to follow the instructions here I've stumbled. How to proceed?
>> require 'watir-webdriver'
=> true
>> require 'webdriver-user-agent'
=> true
>> driver = UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
NameError: uninitialized constant UserAgent
from (irb):3
from /usr/bin/irb:12:in `<main>'
Try using Webdriver::UserAgent

Trying to run Hash.to_xml, but Ruby complains?

I'm just trying to run a very simple snippet on my RVM Ruby 1.9.2 installation:
require 'rubygems'
require 'active_support'
obj = {"foo" => "bar"}
xml = obj.to_xml
Ruby complains as follows:
NoMethodError: undefined method `to_xml' for {"foo"=>"bar"}:Hash
from (irb):2
from /Users/.../.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>'
Why is this happening? Isn't to_xml a method of Hash?
require this:
require "active_support/all"

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Why the autoload method cannot find the file in my library?

I'm packaging my library into gem. This is the structure of the project.
|~lib/
| |~renren_api/
| | |-authentication.rb
| | |-http_adapter.rb
| | \-signature_calculator.rb
| \-renren_api.rb
|+spec/
|-README
\-renren-api.gemspec
I write the "lib/renren-api.rb" as follow. Rack inspire me.
module RenrenAPI
VERSION = [0, 3, 1]
def self.version
VERSION * "."
end
autoload :Authentication, "renren_api/authentication"
autoload :SignatureCalculator, "renren_api/signature_calculator"
autoload :HTTPAdapter, "renren_api/http_adapter"
end
Why the autoload method cannot find the required file, but Rack's can?
ruby-1.9.2-head :001 > require "renren_api"
=> true
ruby-1.9.2-head :002 > RenrenAPI::Authentication
LoadError: no such file to load -- renren_api/authentication
from (irb):2
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
ruby-1.9.2-head :003 > RenrenAPI::HTTPAdapter
LoadError: no such file to load -- renren_api/http_adapter
from (irb):3
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
# The following split into newlines to make more readable
ruby-1.9.2-head :004 > $:
=> ["/Users/siegfried/.rvm/gems/ruby-1.9.2-head/gems/renren-api-0.3.1/lib",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/vendor_ruby/1.9.1",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/vendor_ruby",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/x86_64-darwin10.6.0"]
My gemspec file is as follow.
Gem::Specification.new do |spec|
spec.name = "renren-api"
spec.version = "0.3.1"
spec.summary = "a library to request Renren's API"
spec.description = <<-EOF
renren-api provides capability to request the service of Renren Social Network.
EOF
spec.files = Dir["{lib/*,spec/*}"] + %w{README}
spec.require_path = "lib"
spec.extra_rdoc_files = %w{README}
spec.test_files = Dir["spec/*_spec.rb"]
spec.author = "Lei, Zhi-Qiang"
spec.email = "#my email"
spec.homepage = "https://github.com/siegfried/renren-api"
end
When require "renren_api/authentication".
ruby-1.9.2-head :001 > require "renren_api/authentication"
LoadError: no such file to load -- renren_api/authentication
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):1
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
Add ".rb" will not help.
ruby-1.9.2-head :001 > require "renren_api"
=> true
ruby-1.9.2-head :002 > RenrenAPI::Authentication
LoadError: no such file to load -- renren_api/authentication.rb
from (irb):2
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
I find the problem is I made a wrong gemspec.
spec.files = Dir["{lib/*,spec/*}"] + %w{README}
It didn't package the file under "lib/renren_api/".
To change it like this will solve this problem.
spec.files = Dir["{lib/**/*,spec/*}"] + %w{README}

Resources