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.
Related
I'm trying to scrape a table from a website.
status = recPage.css("#MainContent_GridView1").css("tr")[line].css("td")[2].text.chomp.strip
And I got this error for some of the rows.
undefined method `strip' for nil:NilClass (NoMethodError)
or
undefined method `chomp' for nil:NilClass (NoMethodError)
So I thought I could use chomp!.strip! to skip the nil values. But apparently it won't allow me to put those 2 into one line.
Is there a way to modify this?
How about:
status = recPage.css("#MainContent_GridView1").css("tr")[line].css("td")[2].text.chomp.strip rescue ""
It will set status to "";
You're getting these errors because there're nodes with .text=nil, also chomp! and strip! are meant to modify string in-place, not to ignore errors, and they return nil.
Best way is to check the presence of text (not oneliner, thought):
txt = recPage.css("#MainContent_GridView1").css("tr")[line].css("td")[2].text
txt = txt.chomp.strip if txt
In ruby 2.3+ you can use:
recPage.css("#MainContent_GridView1").css("tr")[line].css("td")[2].text&.chomp.&strip
rescue ""-method could lead to errors in the future (if there are some other errors in this line)
If you really want to do it destructively, you can do:
...text&.tap(&:chomp!)&.tap(&:strip!)
Completely new to rspec here, as will become evident.
The following rspec file fails:
require_relative( 'spec_helper')
describe GenotypingScenario do
it 'should add genes' do
scen = GenotypingScenario.new
gene = Gene.new( "Pcsk9", 989 )
scen.addGene( gene )
expect( gene.id).to eq( 989 )
ct = scen.genes.count
expect (ct).to equal(1)
expect (5).to eq(5)
end
end
Specifically, the last two expect() lines fail, with errors like this:
NoMethodError: undefined method `to' for 1:Fixnum
Yet the first expect line works fine. And gene.id is definitely a FixNum.
Ruby 2.1.2, rspec 3.0.0, RubyMine on Mac OS 10.9.4.
Any thoughts?
The spacing in your last two expect lines are tripping up the Ruby interpreter.
expect (5).to equal(1)
Is evaluated by Ruby as:
expect(5.to(equal(1)))
When what you really mean is:
expect(5).to(equal(1))
It's the return value from calling expect() that has a method to; RSpec isn't extending the Ruby built-in types. So you should change your last two expectations to read as follows:
expect(ct).to equal(1)
expect(5).to eq(5)
I was following a Rails API tutorial with TDD, when I found a line in the tests that expected a json response not to be empty.
This is how I wrote it:
expect(json).not_to_be_empty
And I got that unfriendly NoMethodError: undefined method 'not_to_be_empty'
I came to the accepted answer on this thread and it opened my eyes.
I then changed the line to:
expect(json).not_to be_empty
I know you could still be looking for the difference, well, welcome to RSpec! I removed the underscore in between not_to and be empty to make two words. It worked like ... good code.
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.
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.
Im trying to define an array of arrays as a Constant in one of my classes, the code looks like this:
Constant = [[1,2,3,4],
[5,6,7,8]]
When I load up the class in irb I get:
NoMethodError: undefined method `[]' for nil:NilClass
I tried using %w and all that did was turn each one into a string so i got "[1,2,3,4]" instead of [1,2,3,4]
how do I define an array of arrays as a constant?
Im using ruby 1.8.7.
When I define the constant in IRB its fine, but when I load up the class with it in i get an error.
require 'file_with_class.rb'
NoMethodError: undefined method `[]' for nil:NilClass
from ./trainbbcode/tags.rb:2
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from (irb):1
That file looks like this:
class TBBC
Tags = [[/\[b\](.*?)\[\/b\]/,'<strong>\1</strong>',#config[:strong_enabled]],
...
[/\[th\](.*?)\[\/th\]/,'<th>\1</th>',#config[:table_enabled]]]
The code you showed works just fine. You're definitely not getting that error message for that particular line. The error is caused elsewhere.
And yes, %w creates an array of strings. To create normal arrays use [] like you did.
Edit now that you've shown the real code:
#config is nil in the scope where you use it, so you get an exception when you do #config[:strong_enabled].
Note that inside of a class definition but outside of any method definition #foo refers to the instance variable of the class object, not that of any particular instance (because which one would it refer to? There aren't even any instances yet, when the constant is initialized).
It's a bit strange to use a TitleCase name for a constant. But regardless, it works for me:
$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.7.0]
$ irb --version
irb 0.9.5(05/04/13)
$ irb
irb(main):001:0> Constant = [[1,2,3,4],[5,6,7,8]]
=> [[1, 2, 3, 4], [5, 6, 7, 8]]
I also tested it in Ruby 1.9.1. Could you be more specific?