Ruby require causing undefined local variable - ruby

I am a complete noob to Ruby. I wish to execute a file that I have taken from an Amazon AWS IoT page, but I have problems with the initial require as below.
I am using Ubuntu 18.04, I have installed ruby with sudo apt-get install ruby-full , and also sudo gem install mqtt . The file is called iot-connector.rb. I execute the file with ruby iot-connector.rb . It's obvious that it cannot find these require files, but how do I tell Ruby to find them. Thanks.
require ‘rubygems’
require ‘mqtt’
#more code....
The error I get :
Traceback (most recent call last):
iot-connector.rb:1:in `<main>': undefined local variable or method `‘rubygems’'
for main:Object (NameError)

You use wrong type of quotes, use regular ones instead of backquotes:
require 'rubygems'
require 'mqtt'
Backquotes are used to run shell commands, btw. it's quite strange you get ruby errors here, I'd expect sth like:
[29] pry(main)> `rubygems`
Errno::ENOENT: No such file or directory - rubygems

Related

Trying to use line comand in gem: gem install shopify_theme

I am new to ruby and am trying to use the line command:
gem install shopify_theme
However, I get an error
NameError: undefined local variable or method 'shopify_theme' for main:Object
from <rb>:2
from C:/Ruby21/bin/irb:11:in '<main>'
I assume this has something to do with my local files? I've used Dir.chdir to target my folder. Also, I have the gem file here as well.
ref: https://github.com/Shopify/shopify_theme
Thank you and any help is greatly appreciated!
You may be in the ruby console. Exit the ruby console and use cmd.exe!

NameError when trying to run simple ruby commands from irb

Anyone know why I would get this error: " NameError: undefined local variable or method `list' for main:Object
from (irb):1"
anytime i try to run a ruby command such as "gem list" from irb?
excuse my ignorance , ik nothing about Ruby and all im trying to do is install cocoapods to use with my ios projects.
It's because gem list is not "ruby command". It's shell command to list all ruby gems installed on your machine. You should use this outside ruby's irb console.

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.

Undefined method 'request_uri'

I am working with the Shopsense Ruby Gem however I am unable to use the gem. Making requests exactly as in the test I receive the following error:
/Users/rudolph9/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:455:in `block in get_response': undefined method `request_uri' for #<URI::Generic:0x007fd5b3a66810> (NoMethodError)
from /Users/rudolph9/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /Users/rudolph9/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:454:in `get_response'
from /Users/rudolph9/.rvm/gems/ruby-1.9.3-p0/gems/Shopsense-0.1.1/lib/Shopsense.rb:76:in `do_search'
from test_shopsense.rb:10:in `<main>'
However, the test works fine if I clone the repository, modify the test and require the source directly as follows:
1 #!/usr/bin/env ruby¬
2 require 'rubygems'¬
3 #require 'Shopsense'¬
4 require '../lib/shopsense.rb'¬
.
.
.
What is the issue when using the gem it self??
Do you have an older version of the gem installed, or another gem which is named Shopsense?
With the error you're getting, I suspect the issue would be related ruby trying to automatically find the gem when you do require 'Shopsense' and finding something other than what you want. When you do require '../lib/shopsense.rb' it defines a specific path to the gem, so you always get the gem you want.
I don't have any experience with Shopsense, but the link you supplied looks like its a version 0.1.0, while the ruby interpreter found a Shopsense-0.1.1. I suspect the issue has to do with that. In fact, the source for Shopsense you linked to, line 76 (where the error is in your output) is a blank line.

Can not find gems running "ruby <my_script>.rb" – but works in IRB

I am experiencing issues when I am trying to run my .rb-file with the Ruby-command trying to access a gem. The gem i am trying to use is Ruby-Whois. I have an example script below that when I try to execute it through "ruby whois.rb" I get this error message:
./whois.rb:6: uninitialized constant Whois (NameError)
However, if I run the same script line by line in IRB I get the expected result. What may cause this?
Below is whois.rb
require "rubygems"
require "whois"
domain = "google.com"
c = Whois::Client.new
a = c.query(domain)
puts a
change the name of your file - there is ambiguity in require 'whois' and ruby is requireing your file instead of a gem. when you do it line by line in irb ruby knows what you exactly want to require, so everything works.

Resources