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?
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.
main at all if you don't want to. Your script could have one line containing simply: puts "Hello World!"main, there are no functions in Ruby. Also, we don't talk about "calling methods" in Ruby, we talk about "sending messages".