Ruby methods confusion - ruby

If I run:
myObj.methods - Object.methods
I see the method I expect listed in the results:
[:myMethod]
If I then run:
myObj.myMethod()
I get the following error:
symbol lookup error ... undefined symbol: myMethod
What is happening? Am I calling myMethod correctly?

Related

Why is RSpec giving a NoMethodError when method exists?

I'm getting the error below
$ bundle exec rspec calculatorcli_spec.rb
Failure/Error: c = CalculatorCLI.parse
NoMethodError:
undefined method `parse' for CalculatorCLI:Class
from the below simple calculator class
CalculatorCLI is a class, and you're missing its initialization
either change CalculatorCLI.parse to CalculatorCLI.new.parseor changedef parsetodef self.parseinCalculatorCLI` definition

undefined method constantize in rake task

I'm trying to run this code"
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.constantize.each do |factory|
factory.public_send(:delete_all)
end
end
But I get this error: NoMethodError: undefined methodconstantize' for #`
Do you know how I can solve the issue?
In ruby uppercased variables are constants by default so you can't call constantize on it. In your case it's just an array so this should work:
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.each do |factory|
factory.constantize.public_send(:delete_all)
end
You can call String#constantize only on strings but you are calling it on array FACTORY.
Remove FACTORY.constantize and add factory.constantize.public_send(:delete_all)
Also make sure you have ActiveSupport::Inflector required

Did this method 'method' throw this error message?

The ruby code
class Word < Array
def g
puts "JJ"
end
end
w = Word.new([4, 6])
puts Word.method(:g)
produces the following error:
main.rb:9:in `method': undefined method `g' for class `#<Class:Word>' (NameError)
from main.rb:9:in `<main>'
because g is an instance method for Word, not its class method.
It looks like it is the method method that in fact threw this error message. I wouldn't be able to explain how such an error message could be produced if that were not the case.
Is it in fact the method method that threw this error message?
Here, we are passing the symbol :g to the method Word.method, and waiting for its execution. The ruby interpreter cannot know beforehand that it's gonna be an error, only when the method method checks for the existence of a function with a name similar to the symbol :g can it (the method) decide that an error exists. The console/interpreter cannot know about the error beforehand.
Yes. The description:
in `method'
in the error message means exactly that.

Ruby 2.1 and mislav-will_paginate 2.3.10 shows ERROR: NoMethodError: undefined method `paginate'

Does anyone know what I'm doing wrong here, I installed Ruby 2.1, Sequel-4.26.0 gem and mislav-will_paginate-2.3.10 gem, but when i try to use the paginate function, i keep getting the following error:
Code:
#user = User.paginate(:page => 1, :per_page => 2)
Error message:
"ERROR: NoMethodError: undefined method `paginate' for #"
Most likely paginate is a dataset method, not a class method (this is true for Sequel's pagination extension, not sure about will_paginate). If you want User.paginate to work:
def User.paginate(*args)
dataset.paginate(*args)
end

Ruby. Crack gem. -- in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) --

Dear stackoverflow community,
Beginner's question:
Why do I get the following error?
scraper_sample_2.rb:7:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
>Exit code: 1
Here's my code (copied from a ruby's intro guide):
require "rubygems"
require "crack"
require "open-uri"
URL = "http://www.recovery.gov/pages/GetXmlData.aspx?data=recipientHomeMap"
Crack::XML.parse(open(URL).read)["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end
Because Crack::XML.parse(open(URL).read)["totals"] is nil. Try to split the call you do on line 7 on several lines and debug each call separately. Maybe the answer you get is not what you expect.
Given the format of the xml returned from your source, Crack::XML.parse(open(URL).read)["totals"] will, as Ivaylo said, return nil. The format of the xml must have changed, as totals are now within /map/view.
To get the expected output, change your code to:
Crack::XML.parse(open(URL).read)["map"]["view"]["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end

Resources