Ruby hides lots of stuff.
Ruby is OO like C++, Objective C and Java, and has main
like C but you don't see this.
puts(42)
is method call. It is a method of the main object called main
. You can see it by typing puts self
.
If you don't specify the receiver (receiver.method()) Ruby will use the implicit one, main
.
Check available methods:
puts Object.private_methods.sort
Why you can put everything anywhere?
C/C++ look for main method called main
, and when C/C++ find it, it will be executed.
Ruby on other hands doesn't need main
or other method/class to run first.
It execute code from the first line until it meet the end of file(or __END__
on the separate line).
class Strongman
puts "I'm the best!"
end
is just syntactic sugar for Class.new
method:
Strongman = Class.new do
puts "I'm the best!"
end
The same goes for 'module`.
for
calls each and returns some kind of object. So you may think of it as something similar to method.
a = for i in 1..12; 42;end
puts a
# 1..12
In the end, it doesn't matter if it is method call or some kind of structure like C's int main()
. Programming language decides what it should run first.