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
Related
I'm trying to call the sh method from FileUtils.
class A
def self.method
sh('echo hello')
end
end
def function
sh('echo hello')
end
function # this is fine
A.method # this gives an error
The error I get is
NoMethodError: undefined method 'sh' for A:Class
How can I change my method to be able to call the function?
I have the following code structure:
class Test
def self.test
def something
return 'test'
end
#test = something()
print(#test)
end
end
If I try to run this code, Ruby gives me this error:
test.rb:33:intest': undefined method something' for Test:Class (NoMethodError)
How can I call this function from within the given scope?
The scope you are referring to is that of the class - Test. The method something is actually getting defined as the instance method of the class Test. You cannot really invoke an instance method from a class scope, hence the error.
I want to trap invocation of a method and then display output.
class A
end
If i run new A.see it should trap and print 'unkown method'.
I am new to the language
When you send a message to an object, the object executes the first method it finds on its method lookup path with the same name as the message. If it fails to find any such method, it raises a NoMethodError exception, unless you have provided the object with a method called method_missing. The method_missing method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method.
class A
def method_missing(m, *args, &block)
puts "There's no method called #{m} here -- please try again."
super
end
end
This already triggers a NoMethodError that by default halts your program.
NoMethodError: undefined method `see' for A:Class
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.
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