I am wondering if it is possible to call class methods within 'main' method in Ruby.
Below codes produces an error.
test.rb
class Client
def printtwo
puts 2
end
if __FILE__ == $0 #if this file gets run by an interpreter, run below codes
#just like main() method in java
printtwo() #this throws below error
#undefined method `printtwo' for Client:Class (NoMethodError)
end
end
While codes without class declaration runs fine if I run them
test2.rb
def printtwo
puts 2
end
if __FILE__ == $
printtwo() #this prints 2
end
The reason that I want to use this executable inside a class declaration is because, I want to use class variables by setting attr_accessor
. I believe this attr_accessor
has to be used in a class. right?
How can I resolve this problem so that method call will not produce an error?