undefined method data_for for main:object error in Ruby - ruby

I wrote a small script that try to read data from a .yml file using DataMagic.
require 'watir-webdriver'
require 'data_magic'
DataMagic.yml_directory="/home/krishna/RUBY/"
DataMagic.load "testdata.yml"
testData = data_for("testdata.yml/Testcase_01")
puts "#{testData[:username]}"
when i execute this i am getting an error
hashdata.rb:7:in `<main>': undefined method `data_for' for main:Object (NoMethodError)
Tell me what i am missing. Thanks.

require 'watir-webdriver'
require 'data_magic'
include DataMagic
DataMagic.yml_directory="/home/krishna/RUBY/"
testData = data_for("testdata/Testcase_01")
puts "#{testData['username']}"

Related

`<main>': undefined local variable or method `database' for main:Object (NameError)

I want to connect MongoDB with Ruby.
I've used the following Ruby file:
require 'rubygems'
require 'mongo'
#con = Mongo::Connection.new
#db = #con['tutorial']
#users = #db['users']
When I use the command ruby connect.rb I get this error:
undefined local variable or method 'database' for main:Object (NameError)
As clearly specified in the documentation, the connection should be established that way:
#con = Mongo::Client.new('mongodb://127.0.0.1:27017/tutorial')
#users = #con[:users]

undefined method `[]' for nil:NilClass while using Nokogiri

I am using Nokogiri to scrape data from a HTML document, but I'm running into the following error:
`block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
This is the code to reproduce the problem:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.somewebsite.com/somepage/some"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".Info_listing").each do |x|
puts x.at_css(".MoreInfo")[:href]
end
Does anyone know why I'm getting this error?
at_css will return nil if there's no matching element.
If you want to get MoreInfo class element inside Info_listing-class element, you'd better to use following code:
doc.css(".Info_listing .MoreInfo").each do |x|
puts x[:href]
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

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"

Undefined method `name' for nil:NilClass (NoMethodError) when running script

When I run the following script to retrieve the first page of google results
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.google.co.uk/search?q=stackoverflow'))
doc.css('div.vsc').each do |element|
puts element.at_css("h3.r a.l").content
end
I get a undefined methodcontent' for nil:NilClass (NoMethodError)`
How could I solve that? Or at least how could avoid it showing when executing?
As Dave Newton already pointed out in his comment, the result of at_css("h3.r a.l") is nil in your case. Neither the NilClass nor the object nil have a method content.
Workaround:
doc.css('div.vsc').each do |element|
next unless elem = element.at_css("h3.r a.l")
puts elem.content
end

Resources