Error message

I’m a beginner of Ruby at the age of 70s. Please help.

I was trying to execute a very elementary example below taken from the
standard textbook called “tanoshii Ruby” , but I couldn’t and got an
error message “uninitialized constant HelloWorld (NameError)” instead.

class << HelloWorld
def hello(name)
puts “#{name} said hello.”
end
end

HelloWorld.hello(“John”)

Any suggestion?

Yoshi

Hello Yoshi,

if you look at these docs:
http://ruby-doc.org/core-2.3.1/doc/syntax/modules_and_classes_rdoc.html

at the bottom of the page there’s some information about creating
singleton classes. (Singleton pattern - Wikipedia)
Creating a singleton class in Ruby uses a very similar syntax to what
you have written above.

Looking at how you want to use the class, you need a class method, which
you can define using self:

class HelloWorld
def self.hello(name)
puts “#{name} said, ‘hello’”
end
end

Then you can call the method hello on the class, without having to make
a class object.

HelloWorld.hello(“John”)

Which should print:
John said, ‘Hello’

Dear Joe,

Thank you for your quick response. The textbook provides four examples,
which should give the same result, including the one you kindly
described.

The other two are:

A)
class HelloWorld
class << self
def hello(name)
puts “#{name} said hello.”
end
end
end

B)
def HelloWorld.hello(name)
puts “#{name} said hello.”
end

The definition of A) and the one you provided worked OK, but B) and the
one I mentioned in my original enquiry didn’t work. The textbook uses
the latest version of Ruby(v2.3) as I do.

Yoshi

Good morning Yoshi,

OK. Well, the reason why these examples are not working is because they
are only excerpts from a program. Try this, for example

class HelloWorld
end

class << HelloWorld
def hello(name)
puts “#{name} said hello.”
end
end

HelloWorld.hello(“Bill”)

Now you should get the expected output. For somebody coming from older
computer languages this is unusual but in ruby you can reopen classes,
even classes from the core library, which is often known as “monkey
patching”.

I’m not sure, what the book is trying to explain with these examples–
perhaps that “everything in ruby is an object”, even the class, class?
This is a very interesting part of the language, which leads to
metaprogramming. And maybe this article, will interest you:

On the other hand, it is quite a complicated subject and it isn’t
necessary to know exactly how this works when you are beginning to learn
the language. I think it depends on how you learn: If you are somebody,
who requires detailed information about every aspect of the language,
you will have to persevere, but ruby can be very deep in this regard. On
the other hand you can make a mental note about this type of syntax and
that it has something to do with metaprogramming and return to it later.

I hope this was some help. Maybe someone else can add a reply, who knows
more about the book you are using.

Dear Joe,

Thank you for your elaborated explanation, which solved my problem.

I knew the textbook omitted the last line, i.e.HelloWorld.hello(“Bill”),
but didn’t know the first two lines were also omitted. The book is in
Japanese and its title can be translated as “Pleasant Ruby”.

I’m just learning programming for fun and chose Ruby as I thought it
would be the simplest OOP language.

Yoshi