Ruby Language Getting started with Ruby Language My First Method

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Overview

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

  • def is a keyword that tells us that we're def-ining a method - in this case, hello_world is the name of our method.
  • puts "Hello world!" puts (or pipes to the console) the string Hello world!
  • end is a keyword that signifies we're ending our definition of the hello_world method
  • as the hello_world method doesn't accept any arguments, you can omit the parenthesis by invoking the method


Got any Ruby Language Question?