Category Archives: Ruby

Ruby Tutorial

Ruby tutorial

Ruby Tutorial – Learn how to use Ruby programming language. Ruby is an object-oriented programming language. The basic topics of Ruby are: variables, strings, methods, blocks, exceptions, arrays, loops and other control statements. Ruby tutorial Ruby Variables Ruby Strings Ruby Date and Time Ruby Array Ruby Hashes Ruby Ranges Ruby If-Elsif-Else Ruby Case Ruby For Loop Ruby While… Read More »

Ruby Exceptions

Ruby Exceptions The Ruby exceptions syntax and example. Exceptions syntax begin –Ruby code rescue –Ruby code end Exceptions example begin puts ‘Start block’ raise ‘Raise Error!’ puts ‘End block’ rescue puts ‘Exception Handling with rescue’ begin puts ‘Block in exception’ end end Output Start block Exception Handling with rescue Block in exception Exceptions example begin puts ‘AAA’ raise… Read More »

Ruby Blocks

Ruby Blocks The Ruby blocks syntax and example. Blocks syntax block_name{ — Ruby code } –or do — Ruby code end Blocks example def test_block puts “Method A” yield puts “Method B” yield 123 puts “Method C” yield “abc” end test_block {|x|puts “The block #{x}”} Output Method A The block Method B The block 123 Method C The… Read More »

Ruby Methods

Ruby Methods The Ruby methods syntax and example. A Ruby method consists of: The def keyword Method name The body of the method Return value The end keyword Methods syntax def method_name — Ruby code end –or def method_name(value_1, value_2) — Ruby code end Methods example def test_method a = 1 b = 2 c = 3 d… Read More »

Ruby Ranges

Ruby Ranges The Ruby range represents an interval of values. The syntax of Ruby range allow you to include or exclude its ending value. Ranges syntax (1..10) (1…10) Ranges example r1 = (1..5).to_a r2 = (1…5).to_a r3 = (1..5).to_a.reverse puts “#{r1}” puts “#{r2}” puts “#{r3}” Output [1, 2, 3, 4, 5] [1, 2, 3, 4] [5, 4, 3,… Read More »

Ruby Hashes

Ruby Hashes Ruby Hash is a collection of key-value pairs. The Ruby Hash is created using key-value pair within {}. Hashes syntax myMethod = “myHashName” ) Hashes example myMethod = “myHashName” ) myHashName = {“10” => “A”, “20” => “B”, “30” => “C”} puts “#{myHashName.keys}” Output [“10”, “20”, “30”] Hashes Elements example myHash = { “a”… Read More »

Ruby Array

Ruby Array The Ruby array is created using the objects between [ and ]. Ruby arrays are ordered collections of elements. Array syntax myArrayName = Array new class method example myArrayName = puts myArrayName.size puts myArrayName.length Output 30 30 Array Elements example arrName = [“A”, “B”, “C”, “X”, “Y”, “Z”, “1”, “2”, “3”] puts puts… Read More »

Ruby Date and Time

Ruby Date and Time The Ruby Date, DateTime and Time examples. Date and Time syntax Date DateTime Time Date and Time example cTime = puts “Current Time : ” + cTime.inspect puts cTime.year puts cTime.month puts require ‘date’ puts puts Date.parse(‘2018-09-22′) puts Date.strptime(’22-09-2018’, ‘%d-%m-%Y’) puts,11,13).to_date Output Current Time : 2018-09-22 09:36:56 +0300 2018 9… Read More »

Ruby Strings

Ruby Strings The Ruby strings syntax and example. Strings syntax puts “This is a string.” puts ‘String syntax’ Strings example puts “This is a string.” puts ‘String syntax’ puts “abc – 123” puts “1234567” puts “Concatenate string A” + “BCDE” puts “Ruby string. \nTest new line.” a, b = 2, 3 puts “A = #{ a }” puts… Read More »

Ruby Variables

Ruby Variables The Ruby Variables syntax and examples. Local Variables syntax Local Variables variable_name = variable_value Global Variables $variable_name = variable_value Class Variables Instance Variables Local Variables example irb(main):001:0> x=1 => 1 irb(main):002:0> x => 1 irb(main):003:0> Variables example x=1 puts x y=’test rubyyy’ puts y