Reputation: 73
I have the following code in ruby:
Class Sample
def hello
puts "Hello"
end
s = Sample.new
s.hello
The error I'm getting is
my_prog.rb:1:in '': uninitialized constant Sample (NameError).
Why am i getting this error message?
Upvotes: 0
Views: 262
Reputation: 545
There is a silly mistake in your code. You should change a little bit code. you have given Class Sample instead of class Sample so ruby interpreter could not find Sample class in my_prog.rb while you are calling a new object of Sample class You can use below code
class Sample
def hello
puts 'Hello'
end
end
sample = Sample.new
sample.hello
Upvotes: 2
Reputation: 36101
Your syntax for declaring classes is invalid. Try:
class Sample
def hello
puts 'Hello'
end
end
Sample.new.hello
Also note that unlike languages like java, you don't need a "main" class/method.
puts 'Hello'
will suffice
Upvotes: 6