Is def greet; puts "hello"; end
the only way to define a method on one line in Ruby?
Defining & Calling the method: In Ruby, the method defines with the help of def keyword followed by method_name and end with end keyword. A method must be defined before calling and the name of the method should be in lowercase. Methods are simply called by its name.
You can get a reference to the method by object. method(:method_name) . Eg: To get a reference to system method.
The code def hi starts the definition of the method. It tells Ruby that we're defining a method, that its name is hi . The next line is the body of the method, the same line we saw earlier: puts "Hello World" . Finally, the last line end tells Ruby we're done defining the method.
@Hauleth's answer is correct: there is no main method or structure in Ruby.
You can avoid the need to use semicolons if you use parentheses:
def hello() :hello end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With