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?
Related
i have two methods in base class, method_1 does perform_async with some *args by calling the worker and after job is done it will call method_2, how to pass same args to method_2?
def self.method_1(action_name, *args)
perform_async(action_name,args)
method_2
end
def self.method_2
some action here
method_1(action_name, args)
handle exception here
end
getting error undefined local variable or method `action_name' in line number 3 of method 2.
If I'm understanding what you're going for, what about using an instance variable to store the splat parameter arguments?
def self.method_1(action_name, *args)
#args = args
perform_async(action_name, args)
method_2
end
def self.method_2
some action here
method_1(action_name, #args)
handle exception here
end
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
I'm using the following snippet to only alias the method if the method exists:
alias_method old_name, func_name if self.respond_to? func_name
How can it Error with alias_method': undefined method 'get' for class 'Sinatra::Base' (NameError) when self.respond_to? func_name returns true?
func_name = :get in this snippet
I found a good answer to my problem here at this link
Instead of monkey patching the original class I instead create a module and then use prepend and just call super to call the original method.
I now do this:
Sinatra::Base.prepend Restman::Patches::Sinatra_Base_Patch
With the module Sinatra_Base_Patch containing the function that overwrites the original.
The example I followed is this:
class Foo
def bar
'Hello'
end
end
module FooExtensions
def bar
super + ' World'
end
end
class Foo
prepend FooExtensions # the only change to above: prepend instead of include
end
Foo.new.bar # => 'Hello World'
I bet you call alias_method inside the scope of Sinatra::Base. You should call it inside the singleton class of Sinatra::Base, because the method get is defined as a class method.
The syntax is:
alias_method :new_name, :func_name
E.g., you have a name attribute in a record:
alias_method :to_s, :name
I am executing a class with only this code in rubymine:
def saythis(x)
puts x
end
saythis('words')
It returns an error: undefined method `saythis', rather than printing the string 'words'. What am I missing here? Replicating this code in irb prints the string 'words'.
I assume you wrote a class like the one below and did not write that code into a irb console. The problem is that you define an instance method, but try to call the method from the class level.
class Foo
def say_this(x) # <= defines an instance method
puts x
end
say_this('words') # <= calls a class method
end
There a two ways to "fix" this:
Define a class method instead of an instance method: def self.say_this(x)
Call the instance method instead of the class method call: new.say_this(x)
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.