Method Basics
In this page:
Method Basics
A Ruby method is defined with def method_name ... end, and called by its name, with or without parentheses depending on context. Method names conventionally use snake_case, and by convention, a method ending in ? returns a boolean while one ending in ! performs a mutating or "dangerous" operation. Every method returns a value, either explicitly via return or implicitly as the value of its last expression.
Note: Parentheses around method arguments are optional in Ruby, but idiomatic style generally includes them for methods that take arguments.
Example: Method Basics
def greet
puts "Hello from a method!"
end
greet
greet()
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: