# 1. Ruby adalah bahasa yang Object Oriented (OOP) => semua elemen dalam source code adalah object
# 2. OOP => Class dan object
# 3. Object => atribut; method/function

puts "Sedang belajar ruby"

# Variable
numeric_1 = 1 #Integer, Float, Decimal
numeric_2 = 10
numeric_3 = 10.5

string_1 = "halo"
string_2 = "3"
boolean_1 = true # atau false

array_1 = [1,2,3,4,5]
array_2 = [true, false]
array_3 = [1,2, true, "halo"]
array_4 = [1,2, [3,4], [5, [6]]]

hash_1 = {"satu" => 1, "dua" => 2, "tiga" => 3, "four" => 4, "five" => 5}
hash_2 = {satu: 1, dua: 2, tiga: 3}
hash_3 = {"satu" => 1, "dua" => {"dua-satu" => {"dua-satu-satu" => 122, "dua-satu-dua" => 212}}, "tiga" => 3}

range = 1..5
sekarang = Time.now

numeric_3 = numeric_1 + numeric_2 + string_2.to_i
puts numeric_3

jumlah = (numeric_3 + numeric_2).to_i
puts jumlah

# + penjumlahan
# - pengurangan
# * perkalian
# / pembagian
# % modulo

=begin
# Constant
Constant_1 = 1

puts numeric_1.class
puts numeric_2.class
puts boolean_1.class
puts hash_2.class

puts "Hash (tipe data) ----------"
puts hash_3["dua"]
puts hash_3.map {|key, value| puts key }

puts "Range (tipe data) ----------"
array_range = range.to_a
puts array_range.class
puts range.class

puts "Datetime (tipe data) ----------"
puts sekarang
puts sekarang.year

puts "Konversi (tipe data) ----------"
puts numeric_1.to_s
puts string_1.to_i
puts hash_1.to_a

puts "Konversi (tipe data) ----------"
puts numeric_1 + string_1

puts "Kondisi (if else) ----------"
if (numeric_1 > 0)
  puts "INTEGER #{string_1}"
elsif (numeric_2 < 0)
  puts "NEGATIVE"
else 
  puts "NONE"
end

puts "For do (looping) ----------"
for a in array_3 do
  puts a
end

puts "Each do (looping) ----------"
array_3.each do |a|
  puts a
end

puts "Each with index (looping) ----------"
array_2.each_with_index do |a,i|
  puts "#{i+1} = #{a}"
end

puts "Operasi aritmatika 1 ----------"
array_1.each_with_index do |a,i|
  puts a+1
end

puts "Operasi aritmatika 2 ----------"
array_1.each_with_index do |a,i|
  puts a+array_1[i-1]
end

# 1, 4, 9, 16, 25 (i*i)
puts "Operasi aritmatika 3 ----------"
array_1.each do |element|
  result = element * element
  puts "#{result}"
end


# 1, 3, 5 (ganjil saja)
puts "Operasi aritmatika 4 ----------"
array_1.each do |element|
  if element % 2 != 0
    puts "#{element}"
  end
end

# 2, 4 (habis dibagi 2)
puts "Operasi aritmatika 5 ----------"
array_1.each { |element| puts "#{element}" if element % 2 == 0 }

# Operasi: + - / * %
# Variable: global variable, local variable, instance variable, class variable
# Constant: 

# Constant_1 = 2 #akan error


# Function:
def fungsi_1
  puts "Function 1 ----------"
  array_input = [1,2,3,4,5]
  array_input.each { |element| puts "#{element}" if element % 2 == 0 }
end

fungsi_1()

def fungsi_2(array_input, teks, label="ANGKA")
  puts "Function 2 ----------"
  array_input.each { |element| puts "#{element}" if element % 2 == 0 }
end

#fungsi_2(array_1)
fungsi_2(array_1, "")


# Execercise: 
# array_3 = [1,2, true, "halo"]
# fungsi_3(array_3, "Billy")
# "halo Billy"

# fungsi_3(array_3, "Andi")
# "halo Andi"
def fungsi_3(array, nama)
  array.each do |element|
    if element.is_a?(String)
      puts "#{element} #{nama}"
    end
  end
end

array_3 = [1, 2, true, "halo"]
fungsi_3(array_3, "Billy")
=end 
by

Ruby Online Compiler

Write, Run & Share Ruby code online using OneCompiler's Ruby online compiler for free. It's one of the robust, feature-rich online compilers for Ruby language, running on the latest version 2.3.1. Getting started with the OneCompiler's Ruby compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Ruby and start coding.

Read input from STDIN in Ruby

OneCompiler's Ruby online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Ruby program which takes name as input and prints hello message with your name.

name = gets.chomp               
print "Hello #{name}.\n" 

About Ruby

Ruby is a general purpose object oriented programming language developed by Yukihiro Matsumoto.

Key features

  • More commonly used in Rails applications.
  • Concise and simple to read and powerful too.
  • Open-source
  • Expressive features and visual appearance
  • Flexible language

Syntax help

Data types

Data typeDescriptionUsage
FixnumRepresents normal numbersx = 10
BignumRepresents big numbersx =9999999999
FloatRepresents decimal numbersx = 3.14
ComplexRepresents imaginary numbersx = 1 + 2i
RationalRepresents fractional numbersx = 1/4
BigDecimalRepresents precision decimal numbersx=1.0
HashRepresents key value pairs{"Website"=>"onecompiler","message" => "Happy learning"}

Variables

In Ruby, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically and a prefix is needed to indicate the following variable types.

Variable typeDescription
Local variablesScope is limited to the block of the variable initialization. Variable name must start with either _ or lowercase letter.
Class variablesClass variables belongs to whole class and can be accessible anywhere inside the class. Variable name must start with @@. They must be initialized before use.
Instance variablesInstance variables belongs to a instance of the class. They can be accessed from any instance of the class within a method. Variable name must start @
Global variablesScope is global and hence they can be accessible anywhere in the program. Variable name must start with $

Loops and conditional statements

1. If family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression)
    #code    
end

If-else

if(conditional-expression)  
    #code if condition is true  
else   
    #code if condition is false  
end 

Nested-If-else

if(condition-expression1)   
    #code if above condition is true  
 elsif(condition-expression2)  
    #code if above condition is true  
elsif(condition-expression3)   
    #code if above condition is true  
...  
else   
    #code if all the conditions are false  
end  

2. Case:

Case is similar to Switch statement, where it is used to execute one set of a statement from multiple conditions.

case expression  
[when expression [, expression ...] [then]  
   # code ]...  
[else  
   # code ]  
end  

3. For:

For loop is used to iterate a set of statements based on a condition.

for variable in expression do   
  # code  
end

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition do   
 # code 
end  

5. Do-while:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

loop do   
  #code  
  break if conditional-expression  
end