Command line: undefined local variable or method - ruby

I got the following code:
require 'base64'
def Decrypt(rsaCipher, encryptedValue)
print base64.decode64(encryptedValue)
end
Decrypt("a", "b")
I get the error message in 'Decrypt': undefined local variable or method 'base64' for main:Object (NameError)'. What am I doing wrong? Why isn't Ruby detecting the base64 library even though I included it?

It should be Base64 but not base64.................................See the doc.

Related

Calculating Averages in Ruby Name Error

I'm getting this:
NameError: undefined local variable or method `sentence_count' for main:Object
when I type this:
puts "#{sentence_count / paragraph_count} sentences per paragraph (average)"
Any suggestions?
NameError raises when you try to use an undefined name. Take a moment to read & understand that the error is telling you sentence_count is undefined.

'ruby-dbus' in TTY terminal

i have the next code:
#! /usr/bin/env ruby
require 'dbus'
bus = DBus::SessionBus.instance
If i execute this code from desktop, it's fine. But if i execute the code in a TTY1-6 terminal i get this output:
home/migue/.rvm/gems/ruby-2.1.2/gems/ruby-dbus-0.11.0/lib/dbus/bus.rb:611:in
'address_from_file': undefined method '[]' for nil:NilClass (NoMethodError)
from /home/migue/.rvm/gems/ruby-2.1.2/gems/ruby-dbus-0.11.0/lib/dbus/bus.rb:600:in 'initialize'
from /home/migue/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/singleton.rb:141:in 'new'
from /home/migue/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/singleton.rb:141:in 'block in instance'
from /home/migue/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/singleton.rb:139:in 'synchronize'
from /home/migue/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/singleton.rb:139:in 'instance'
from bus.rb:3:in '< main >'
Any idea?
The source code reveals this line:
display = ENV["DISPLAY"][/:(\d+)\.?/, 1]
The NoMethodError occurs, because ENV["DISPLAY"] returns nil.
Setting the DISPLAY environment variable in your terminal should fix the problem.

Getting my Ruby file to load into Pry?

I'm trying to edit my Ruby file with Pry. There are few variables that are set in it, and for whatever reason I can't seem to cd into them because they aren't being defined even after I 'load' the file.
Here is the code:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri.XML('<foo><bar /><foo>', nil, 'UTF-8')
url = "http://superbook.eventmarketer.com/category/agencies/"
puts "Finished!"
In Pry I do:
load "./AgencyListingScraper.rb"
and then this is the output:
7] pry(main)> load './AgencyListingScraper.rb'
Finished!
=> true
[8] pry(main)>
Then when I try to do something like:
[8] pry(main)> url
NameError: undefined local variable or method `url' for main:Object
from (pry):6:in `__pry__'
[9] pry(main)> cd url
Error: Bad object path: url. Failed trying to resolve: url. #<NameError: undefined local
variable or method `url' for main:Object>
[10] pry(main)>
This is what I get.
I think I'm not loading the file correctly although I've been searching for hours and I can't figure out how to properly do this. I was doing it right months ago when I had made a scraper with Ruby, but this time I'm having trouble just getting started because of this bit.
Thanks for your help in advance!
Try it this way:
In your file include Pry and do a binding.pry:
require 'nokogiri'
require 'open-uri'
require 'pry'
doc = Nokogiri.XML('<foo><bar /><foo>', nil, 'UTF-8')
url = "http://superbook.eventmarketer.com/category/agencies/"
binding.pry
puts "Finished!"
Then run the file by executing:
ruby AgencyListingScraper.rb
That should drop you into a Pry session where you can use commands like ls to see all of the variables.
Both the way you used Pry, and this way, work. However, the reason that load may not be working in your case is that local variables don't get carried over across files, like when you require one file from another.
Try loading this file:
#test.rb
y = "i dont get carried over cause i am a local variable"
b= "i dont get carried over cause i am a local variable"
AAA= "i am a constant so i carry over"
#per = "i am an instance var so i get carried over as well"
When you load it in Pry using load "test.rb" you can see that you can't get access to the local variables from that file.
I found this question googling but the proposed solution did not work for me because the file I wanted to load was not a class nor a script but a complex ruby config file, so I was not able to inject pry in the code.
But I also found an answer in Reddit linked to this gist that was exactly what I was looking for.
Doing a
Pry.toplevel_binding.eval File.read("stuff.rb")
Will effectively execute the ruby code of the file stuff.rb in the current pry session, leaving the resulting objects for inspecting.

How to execute local functions using code from external file in ruby?

Can a require execute a locally defined function? I guess the easiest way to describe what I need is to show an example.
I'm using ruby 1.9.3, but solutions for 1.8 and 2.0 are also welcome.
I have a file main.rb as the following:
class Stuff
def self.do_stuff(x)
puts x
end
require_relative('./custom.rb')
do_stuff("y")
end
And also have a file custom.rb in the same folder, with the following content:
do_stuff("x")
Running main.rb, I have following output:
/home/fotanus/custom.rb:1:in `<top (required)>': undefined method `do_stuff' for main:Object (NoMethodError)
from main.rb:5:in `require_relative'
from main.rb:5:in `<class:Stuff>'
from main.rb:1:in `<main>'
Note that without the require, the output is y.
I'm not sure if it is the best solution but using eval should do the trick.
class Stuff
def self.do_stuff(x)
puts x
end
eval(File.read('./custom.rb'))
do_stuff("y")
end
The output will be:
pigueiras#pigueiras$ ruby stuff.rb
x
y
In C, #include literally drops the code as-is into the file. require in Ruby is different: it actually runs the code in the required file in its own scope. This is good, since otherwise we could break required code by redefining things before the require.
If you want to read in the contents of a script and evaluate it in the current context, there are methods for doing just that: File.read and eval.

private method `split' called for nil:NilClass (NoMethodError)

I'm trying to use domainatrix with nokogiri and am coming up with a holdup. Being relatively new to ruby, I've tried every syntax variation on the Domainatrix.parse function I can to get the a href's to parse properly. They do print during the "puts" command but when I uncomment the domainatrix code problems start:
require 'rubygems'
require 'domainatrix'
require 'anemone'
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("http://www.cnn.com"))
doc.xpath('//a/#href').each do |node|
linkage = node.text
puts linkage
url = Domainatrix.parse(linkage)
print url.domain
print url.public_suffix
end
Anyone have any ideas on this? I think it is just a syntax issue or perhaps I cannot use the Domainatrix function where I'm using it?
It was getting snagged on some improperly formatted URLs.

Resources