Calling second-level function from the second level in Ruby - ruby

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.

Related

Why am I getting a NoMethodError when calling an instance method from global scope?

I have searched around for the answer to this and I can see a lot of similar problems but I still do not understand what I am doing wrong here. I have declared a Ruby class and attempted to new it and then call some instance methods on the instance, so why do I get the NoMethodError on my start method?
class MyClass
def initialize
self.class.reset
end
def self.reset
...
end
def self.start(port)
...
end
end
test = MyClass.new
test.start '8082' <- here <- undefined method `start' for #<MyClass:0x2f494b0> (NoMethodError)
As you can see I am a Ruby noob. Any help would be appreciated. I can change my class structure but I would really like to understand what I am doing wrong here.
here start is a class method.
By your current approach, you can use it in the following way
MyClass.start '8080'
But if you want to use it on instance of class then use the following code
class MyClass
def initialize
self.class.reset
end
def self.reset
...
end
def start(port)
...
end
end
test = MyClass.new
test.start '8080'
You are using start as a Class variable, the method names preceded with self-keyword make those methods as Class methods. So if you really want to not change your class then you should call it like this:
MyClass.start '8080'
Else you can remove the self from your reset and start methods and make them as Instance methods and use them as:
test = MyClass.new
test.start '8082'

Defining a method in Rubymine returns "undefined method" error

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)

How to call a Method in a Cucumber - Step Definition

I’m a newbie to Cucumber framework. I’m trying to call a Ruby method inside of a step definition. Here is how I define my method in lib/methods.rb
class Test_class
def create_test_scenario()
puts "here!!!"
end
end
This is how I try to call the method inside of a step definition:
And(/^I create scenarios$/) do
Test_class.create_test_scenario
end
I'm getting 'uninitialized constant Test_class (NameError)' when I run the test. Any ideas? Thanks.
You haven't instantiated the Test_class object. For example:
class Test_class
def create_test_scenario
puts "here!!!"
end
end
Test_class.new.create_test_scenario # notice `new` method chained here
#=> here!!!
Errata:
Here's a link to documentation that explains the initialize method and how you can use it to set up object state on initialization.
For class (and module) names, the ruby convention is to use CamelCase. For example, TestClass instead of Test_class
As orde has said, this is down to initialization. To help put the code into context, you would initialize the class object in your step definition as an instance variable (which starts with #). So it would look like this:
And(/^I create scenarios$/) do
#Test_class = Test_class.new
#Test_class.create_test_scenario
end

Call Class Methods to Initialize Static Class

I'm trying to create a singleton class that requires some sophisticated initialization. I've boiled my problem down to this test case:
class Dumb
attr_accessor :mything
#my_thing = 1 # this works
self.init_some_stuff # this gives undefined method
class << self
def init_some_stuff
#my_thing = 2
end
def spill_it
puts "My Thing: #{#my_thing}"
end
end
end
I can initialize simple variables, but want to call class methods to do it, and I get "undefined method". Since I intend it to be used as a singleton, a constructor would not get called. What am I missing?
A method is executed whenever it is met.
self.init_some_stuff
is placed before the definition of it. That is the problem. Place it after the definition.

Rack 'method not found'

I can't seem to see what's up with this piece of code:
class Cherry
class << self
def call env
self::Application.call
end
end
end
class Cherry
class Application
def call env
#Framework logic
end
end
end
run Cherry
That's the part of my application that is not working. I have no idea why:
NoMethodError at / undefined method 'call' for Cherry::Application:Class
You need to adjust a couple of things.
When you are defining the call method inside of Application, you are defining it as an instance method, then you are attempting to call it as a class method, so lets fix the definition to be a class method definition:
class Cherry
class Application
def self.call env
#Framework logic
end
end
end
Next there will be a new error, about not passing the right number of arguments to the call method, so we add the env param to where you are calling the call method.
class Cherry
class << self
def call env
self::Application.call env
end
end
end
Hope that helps!

Resources