Ruby 2.1 NameError: uninitialized constant Curl - ruby

I just finished setting up another development server for my API built using Ruby and Sinatra, however on this server I can't get the curl gem to work properly.
I've installed libcurl and libcurl-devel, and installed the curl gem without any errors, but when I try to use it in code, it always fails. Below is an example in irb:
irb(main):001:0> require 'curl'
=> true
irb(main):002:0> http = Curl.get("http://www.mysuperawesomeapi.com/someendpoint") do|http|
irb(main):003:1* http.headers['accept'] = 'application/JSON'
irb(main):004:1> end
NameError: uninitialized constant Curl
from (irb):2
from /usr/bin/irb:11:in `<main>'
The difference between this development server, and the other one is that this one is using Fedora 21 32bit (hardware limitation) while the other is using CentOS 7 64bit and is a virtual machine. When I try the same code above on irb on the CentOS VM, it works as expected. Any insight would be greatly appreciated.

It looks like in curl (unlike as in curb), there is a class CURL, but not Curl.

Inside gemfile include:
gem 'curl'
gem 'curb'

Related

could not load hiredis extension (undefined symbol: redisReaderFree)

I'm trying to require hiredis in irb and it's already sending me an error:
irb(main):001:0> require "hiredis"
WARNING: could not load hiredis extension, using (slower) pure Ruby implementation.
=> true
irb(main):002:0>
Upon further investigation, I notice that it's crashing at this part of the code.
# hiredis-rb/lib/hiredis/connection.rb
module Hiredis
begin
require "hiredis/ext/connection"
Connection = Ext::Connection
rescue LoadError
warn "WARNING: could not load hiredis extension, using (slower) pure Ruby implementation."
require "hiredis/ruby/connection"
Connection = Ruby::Connection
end
end
So, I required the file hiredis/ext/connection and the error I get is the following error:
irb(main):001:0> require "hiredis/ext/connection"
LoadError: /home/***/projects/***/.gemset/extensions/x86_64-linux/2.1.0-static/hiredis-0.5.2/hiredis/ext/hiredis_ext.so:
undefined symbol: redisReaderFree -
/home/***/projects/***/.gemset/extensions/x86_64-linux/2.1.0-static/hiredis-0.5.2/hiredis/ext/hiredis_ext.so
from /home/***/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I'm using Ubuntu and latest stable version of Redis (2.8.18). How do I fix this? Thanks!
EDIT:
So after looking at the hiredis-rb page: https://github.com/redis/hiredis-rb, it says that I should do the following:
To use hiredis with redis-rb, you need to require
redis/connection/hiredis before creating a new connection.
So I did a require "redis/connection/hiredis" on irb and then require "hiredis" and everything worked fine, no more warnings.
But now the warnings don't happen at all anymore now. I assumed that I'd need to require "redis/connection/hiredis" everytime for it to work but now it just works. So I dunno why it's not warning me anymore now. I thought calling require "redis/connection/hiredis" during that irb session was only for that session, not all succeeding sessions.

JRuby debugger doesn't work as expected

today I gave JRuby a try to use it with Apache's Mahout. But I have a problem with the debugger. When I put the debugger statement on a specific place, it doesn't stop there. It stops later at the end of the script, and then of course none of the variables are available. This is my Gemfile:
source 'https://rubygems.org'
platform :jruby do
gem "jruby_mahout"
gem "ruby-debug"
end
This is the Script:
require 'rubygems'
require 'ruby-debug'
puts "I am called"
debugger
puts "I should not be called before the debugger"
But the output is:
I am called
/usr/local/rvm/gems/jruby-1.7.3#mahout/gems/ruby-debug-base-0.10.4-java/lib/ruby-debug-base.rb:215 warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag
I should not be called
/usr/local/rvm/gems/jruby-1.7.3#mahout/gems/ruby-debug-0.10.4/cli/ruby-debug/interface.rb:129
finalize if respond_to?(:finalize)
(rdb:1)
I'm wondering why there is this warning tracing (e.g. set_trace_func) will not capture all events without --debug flag, because I started the script with jruby test.rb --debug
I installed JRuby 1.7.3 with RVM on my OS X 10.8, and when calling jruby -version I get this output:
jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) 64-Bit Server VM 1.6.0_43-b01-447-11M4203 [darwin-x86_64]
NameError: undefined local variable or method `rsion' for main:Object
(root) at -e:1
Maybe the installation is broken, or I'm doing sth wrong. Any suggestions how to solve this?
I solved it on my own. After a long time of trying, I accidentially putted the --debug statement between:
jruby --debug test.rb
It's weird, but now it works as expected.

Ruby require 'some-gem' works in console, not in Eclipse?

Well, I'm trying to run a simple web server through "rack". So this is my program:
require 'rubygems'
require 'rack'
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]
end
end
Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292
If I run it in the console, it works fine. If I run it in Eclipse, it ends up with error:
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
from /Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/MY_SUPER_SECRET_USER/Sites/service/service.rb:2:in `<main>'
The one that is working is called like this:
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ which ruby
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ ruby service.rb
Then when I try to open localhost:9292 it shows the expected "Hello Rack" code.
I'm on Mac OS X 10.8, with ruby 1.9.3 installed through rvm (this must be obvious). My "rack" package has been installed with sudo gem install rack
So as you can see, the Eclipse is configured with the very same ruby executable. Any suggestions will be of great help!
The line
custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
means it can't find where you installed the rack gem. Reading around the internet, I see again and again using sudo doesn't seem to work. Try just installing it and see if that fixes it.
$ gem install rack (no sudo)
I ran into this same problem, trying to require gems in Eclipse which it wasn't recognizing, even though everything seemed to be configured correctly (the gems were installed, Eclipse was pointing at the right Ruby interpreter, etc).
I ended up making it work by adding GEM_HOME and GEM_PATH variables to the Environment tab of Debug/Run Configurations.
More detailed answer here: https://stackoverflow.com/a/28419300/525338

Ruby Net::SSH undefined method 'shell'

I'm trying to log into a remote machine using SSH in my Ruby code and then trying to cd into another directory once I SSHed in. Here is my Ruby code to do it
require 'net/ssh'
require 'rubygems'
require 'needle'
Net::SSH.start('host', 'shui', :password => "password") do |ssh|
output = ssh.exec!("hostname")
shell = ssh.shell.open
shell.pwd
shell.cd "/svn"
puts shell.pwd
end
when I run my script, it gives me this error:
testssh.rb:8:in `block in <main>': undefined method `shell' ... (NoMethodError)
I'm pretty sure I've installed the gems that are needed to do this. Any suggestions on how to fix this? Thanks!
There is no shell method in Net:SSH v2, the yielded object is a Net::SSH::Connection::Session.
This net-ssh extension gives you the feature you are looking for: https://github.com/mitchellh/net-ssh-shell
Can you check your version?
gem list --local | grep ssh
i have 2.5.2
I found multiple references online, but you should use:
http://net-ssh.rubyforge.org/ssh/v2/api/classes/Net/SSH.html
here is the reference to v1
http://net-ssh.github.com/ssh/v1/chapter-5.html
the later, uses the shell method. But i don't think you have that gem installed.
So have a look at the 1st one (v2). Thats working fine for me.

WEBrick CGI handler pointing to wrong gem location

I am trying to execute my CGI program from WEBrick CGIhandler and kept failing with 'loaderror' of gem libraries. Is someone seen following behavior before? How could I get my CGI scripts to search valid gem location?
[Situaltion]
When I ran CGI scripts that requires 'mysql2' library via Webrick server, I got a following error:
ERROR CGIHandler: /home/charles/code/svr/lib/cgitest.rb:
/home/charles/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mysql2/version (LoadError)
Comment this particular 'require' out from cgitest.rb makes this script work so looks just failed to load this library. Added "puts Gem.path" to the script indicates CGIHandler is looking to following directories to find gem;
["/home/charles/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1", "/.gem/ruby/1.9.1"]
while actual GEM:HOME and GEM:PATH are following;
["/home/charles/.rvm/gems/ruby-1.9.3-p194", "/home/charles/.rvm/gems/ruby-1.9.3-p194#global"]
I think I am missing something very simple but could not figure them out. Really appreciate if someone lead me to the right direction. Any input would be highly welcome.
[Environment]
Using RVM to install Ruby 1.9.3 to Ubuntu Server 12.4 with bunch of gems including "mysql2" library. And the script can load 'mysql2' when I run from local.
$which ruby
/home/charles/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
$irb
1.9.3-p194 :001 > Gem.path
=> ["/home/charles/.rvm/gems/ruby-1.9.3-p194", "/home/charles/.rvm/gems/ruby-1.9.3-
p194#global"]
1.9.3-p194 :002 > require 'mysql2'
=> true
You need to use wrapper instead of bin/ruby:
/home/charles/.rvm/wrappers/ruby-1.9.3-p194/ruby
This will ensure you load proper environment when starting ruby.

Resources