Skip to main content

Ruby Basics

In this section are given basic information about how to save ruby source code file and how to run ruby program. And mentioned basic fundamental detail which are normally used in ruby programming.

Ruby Installation

There are many version are available in ruby programming. In given table are mentioned latest version of ruby.

Version Reference link
Ruby 2.7.0 Ruby 2.7.0 Installation
Ruby 2.6.3 Ruby 2.6.3 Installation
Ruby 2.6.2 Ruby 2.6.2 Installation
Ruby 2.6.1 JRuby 2.6.1 Installation
Ruby 2.5.3 Ruby 2.5.3 Installation

List of Ruby Version

Hello Word

When we are start learning of programming, normally you'll can see the example of hello word. Because that is basic building block to display custom message in console screen. you'll can use other text message. But first view this basic example.

puts "Hello, world"

Ruby source code file are save with the .rb extension. Suppose for example we save this code file using hello.rb. Run of this file by using of the shell prompt in this way.

ruby hello.rb
Hello, world

Ruby are providing various method to display custom message on console screen. There are follows.

puts "Hello Programmer"

p "Hello Ruby Programmer"

print "Hello " 
print "Coder"
Hello Programmer
"Hello Ruby Programmer"
Hello Coder

Ruby Comments

Comment are used in ruby programming to describe the code explanation and short description. All line which are commented by programmer, that lines are ignored at runtime. That means this commented line of code are not execute.

Types of comments

There are two type comment are supported in ruby programming. First is single line comment that is used to describe short description. This comment are start with the # hash symbol.

#This is commented text

Multiline comment are used to describe long explanation and instructions.


Input And Output

puts "Who are you?"
about =gets.chomp
puts "Welcome #{about}" 
Who are you?
Ruby programmer
Welcome Ruby programmer

Ruby Command line argument

Command line argument are used to provide initial value to source code which can be used to primary setup and default setting. This argument value in form of an array that name are ARGV.

puts "#{ARGV}" #Array in form of argument value
for arg in ARGV
   puts arg
Ruby Command Line Argument
#ruby test.rb Hello Ruby Programmer
["Hello", "Ruby", "Programmer"]

Writing functions

def keyword are used to define a method in ruby programming. After that include the name of method, If in case name method name contains two separate words then we can use underscore to combine this word. Because the method name always defining by using single word. That is similar to other programming languages. Here given few valid example to define custom method.

def get_data(id)
    #logic here

def set_amount(amount)
    #logic here

#when no parameter of method then parenthesis is optional
def about
    #logic here 

def add(num1, num2)
    #logic here 

Method scope are terminated by end statement. class method and normal method(without class) both are syntax is similar. class method are used by class object (object.method_name) and which are not define within class this type of function are executed by method name. Internally every methods of ruby are create an objects of method.

def message(data)
    puts data

#execute method
message "0101"

Program flow Control

Using of conditional statements (if, elsif,else) you'll can managed execution flow of program. This statement are base in condition. when condition are true then they are execute conditional block statement. For example.

data = "ruby"
if data == "java"
    puts "Hello Java Programmer"
elsif data == "python"
    puts "Hello Python Programmer"
elsif data == "ruby"
    puts "Hello Ruby Programmer"
    puts "Welcome #{data}"  
Hello Ruby Programmer

There are many other conditional statement are available in ruby programming. Learn more about in Ruby Control Statements.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment