Ruby Tutorial

This tutorial are based on fundamental concepts of ruby programming. There are include operators, variables, loops , control statements, method concepts, class concepts and inheritance etc. The main goal of this ruby tutorial are provide useful information to ruby programmer which are at initial level specially for beginners.

About Ruby Programming

Ruby is open source, scripting, object oriented general-purpose and dynamic programming language. Its creator, Yukihiro Matsumoto (Matz). This language are initially publicly released 1995.

Ruby Features

In this section given small overview of ruby programming features with proper example. If you'll are aware the OOPS concept in any other programming language then you are easily understand to their features and there execution flow of code flow.

Object Oriented

Ruby are flexible programming language there are everything is form of an objects. Even simple primitive values like number, boolean values (true,false), text string are associated with classes. The syntax of defining custom class are very similar to C++, C# and Java programming language.

#Define a ruby class
class Hello
    #class method
    def say(info)
        #statement which are display message
        puts "Hello #{info} :)"
    end
end

#make a object
obj = Hello.new
#execute method by class object 
obj.say("Programmer")
Hello Programmer:)

Ruby modules

That is one of important feature in ruby programming. Module allowing to define methods classes and constant objects. That is very relevant to class but there is cannot create any instance.

Garbage collection

Ruby is one of the programming language which are supports automatic garbage collection. Garbage Collection actually memory space which are occupied by the program execution and objects instances. When execution process are over and objects are out of scope, In this situation that occupied memory are release automatically.

class Teacher
	def initialize(name,age)
		@name = name
		@age  = age
	end
	def infomation()
		puts "Name : #{@name}"
		puts "Name : #{@age}"
		puts "-------------"
	end
end

#basic method
def trainingCamp()

	david = Teacher.new("David",21)
	david.infomation

	#divid, object are local of this method
	#can't use outside the method

end
susen = Teacher.new("Susen",28)
trainingCamp()
susen.infomation
#divid.infomation #not access
Garbage collection example in ruby
Name : David
Name : 21
-------------
Name : Susen
Name : 28
-------------

Ruby’s Flexibility

Ruby are simple syntax and well dynamic data type supported. There are no needs to pre-defined associated data type to objects.

about = 100
#display class
puts about.class.name

about = "Code"
#display class
puts about.class.name

about = 1.2
#display class
puts about.class.name
Fixnum
String
Float

Exception handling

Exception are unwanted condition which are terminates our program abnormally in runtime. Ruby are provides mechanism to control unwanted exception and custom exceptions.

Operator overloading

Operator overloading is a way provide a new meaning of existing operators. Normally operator are used to mathematical and comparison operation. But that is not sufficient for a programmer perspectives. Specially when working on a class objects. That is very simple to overload operator in ruby programming. For example overload + plus operator.

class MyOperator

  def initialize(data)
    @data=data
  end

  def +(other)
    @data = @data + other
  end
  def info()
    puts @data
  end
end

obj = MyOperator.new(1)
obj.info
obj +  2
obj.info
obj +  3
obj.info
1
3
6

You'll cannot override following operators in ruby such as && & || | () {} :: . ~ .. ...

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







© 2021, kalkicode.com, All rights reserved