My First Method
suggest changeOverview
Create a new file named my_first_method.rb
Place the following code inside the file:
def hello_world
puts "Hello world!"
end
hello_world() # or just 'hello_world' (without parenthesis)
Now, from a command line, execute the following:
ruby my_first_method.rb
The output should be:
Hello world!
Explanation
defis a keyword that tells us that we’redef-ining a method - in this case,hello_worldis the name of our method.puts "Hello world!"printsHello world!and a newline.endis a keyword that signifies we’re ending our definition of thehello_worldmethodhello_worldtakes no arguments, so you can omit the parentheses when you call it.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents