2

I have the following code in the script a.rb.

def main
  puts "Hello World!"
end

When I run ruby a.rb on the command line, it doesn't display anything.

Why is this happening?

1 Answer 1

8

Unlike languages like C/C++/Java, Ruby does't have a main method that's called at program startup. The name main is not special.

You defined a method named main, but never call1 it.

def main
  puts "Hello World!"
end

main # here, call the method

1: Technically, calling methods should be called sending messages, the idea comes from Smalltalk.

Sign up to request clarification or add additional context in comments.

2 Comments

For clarity, you don't have to define main at all if you don't want to. Your script could have one line containing simply: puts "Hello World!"
You defined a method called main, there are no functions in Ruby. Also, we don't talk about "calling methods" in Ruby, we talk about "sending messages".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.