I am practicing to code in Ruby and when I type the following code,I get the following error.In this case,what should I do?
The code is here:
class RandomSequence
def initialize(limit,num)
#limit,#num=limit,num
end
def each
#num.times {yield(rand*#limit).floor}
end
end
i=-1
RandomSequence.new(10,4).each do |num|
i=num if i<num
end
http://ideone.com/bSkAXN
the error message I get is:
prog.rb:8:in block in each: undefined method floor for nil:NilClass (NoMethodError)
from prog.rb:8:in times
from prog.rb:8:in each
from prog.rb:14:in <main>
Add parentheses:
#num.times {yield((rand*#limit).floor)}
Without the extra parentheses, yield(rand*#limit) returns nil, and you get a NoMethodError for calling nil.floor.
Related
I'm trying to use Ruby assertion functions, and I keep getting the same error:
undefined method `add_assertion' for nil:NilClass (NoMethodError)
#!/usr/bin/env ruby
require 'test/unit'
require_relative 'functions'
class TestObj < Test::Unit::TestCase
def initialize()
end
def test_me(obj)
for i in 0..2
assert_equal('ok', obj.a_function(input1, input2))
end
end
end
function_session = my_object.new()
test_session = TestValuePair.new()
test_session.test_me(function_session)
I've already tried including the assertion library like so:
require "test/unit/assertions"
include Test::Unit::Assertions
Neither of my objects take any arguments to be initialized. I also know that the my_object object does work.
Anyone have any insight?
Let's consider following code:
class Try
CONST = xxx("42")
private_class_method def self.xxx(str)
str.to_i
end
end
puts Try::CONST
It produces an error: undefined method `xxx' for Try:Class (NoMethodError)
It seems that I cannot use a class private method to initialise a constant. The example is doing nothig actually, but it is a reproduction of an error I am facing trying to read data from file into the class constant.
Is it becuse ruby tries to initialize a constant before it get know about all methodes, or am I doing something wrong?
Not really. What you can't do is to assign the value of something that still hasn't been defined to something else in order to hold its value.
It has nothing to do with the method visibility because this works:
class Try
private_class_method def self.xxx(str)
str.to_i
end
CONST = xxx("42")
end
p Try::CONST
# 42
I'm learning Test Driven Development with Ruby and RSpec. My program should find a given word in the text. The first case should be falsey because the test_word starts with a capital and the second case should be truthy after downcasing it. When I run the spec file though, I get the
undefined methodinclude?' for nil:NilClass`
method and the
undefined method `downcase' for nil:NilClass
error. How can this be resovled?
Here is my code:
strings_spec.rb:
require_relative 'strings'
RSpec.describe BasicString do
before do
#test_word = "Courage"
#sentecne = "Success is not final, failure is not fatal: it is the courage to continue that counts!"
#text = BasicString.new(#sentence)
end
context "case-sensitive" do
it "should output interpolated text" do
result = #text.contains_word? #test_word
expect(result).to be_falsey
end
end
context "case-insensitive" do
it "should output interpolated text" do
result = #text.contains_word_ignorecase? #test_word# 'text & 'test_word' were made instance variables when 'before do' block was added.
expect(result).to be_truthy
end
end
end
strings.rb:
class BasicString
attr_reader :sentence
def initialize(sentence)#The constructor that initializes the instance variable #sentence.
#sentence = sentence
end
def contains_word?(test_word)
#sentence.include? test_word
end
def contains_word_ignorecase?(test_word)
test_word = test_word.downcase#This line downcases the test word.
#sentence.downcase.include? test_word#This test_word is downcased again for the instance variable to be sure it's downcased.
end
end
You have a spelling error in your before block: #sentecne. So, you end up creating your string with a nil value since #sentence hasn't been defined.
I have defined a script with the following code snippet:
check_params param
def check_params(param)
# some code
end
When I run this I get
undefined method `check_params' for main:Object (NoMethodError)
Ruby expects the method to be declared before you call it, try to move your method definition before you call the method like:
def check_params(param)
# some code
end
check_params param
For this piece of code:
class myBaseClass
def funcTest()
puts "baseClass"
end
end
myBaseClass.new.funcTest
I am getting an error:
NameError: undefined local variable or method `myBaseClass' for main:Object
from c:/Users/Yurt/Documents/ruby/polymorphismTest.rb:9
from (irb):145:in `eval'
from (irb):145
from c:/Ruby192/bin/irb:12:in `<main>'
irb(main):152:0> x=myBaseClass.new
When I tryx=myBaseClass.new, I get:
NameError: undefined local variable or method `myBaseClass' for main:Object from (irb):152
Has someone already encountered this problem? I don't think my code can be wrong.
In ruby, all constants including class names must begin with a capital letter. myBaseClass would be interpreted as an undefined local variable. MyBaseClass would work properly.
Your class name should start with a capital, working code below
class MyBaseClass
def funcTest()
puts "baseClass"
end
end
MyBaseClass.new.funcTest
Your code is wrong. Classnames must start with an uppercase in Ruby.
class MyBaseClass
fixes it.
What I don't get is how you don't get a clear error message like I do.
Code working
class MyBaseClass
def funcTest()
puts "baseClass"
end
end
MyBaseClass.new.funcTest