Wednesday, 23 November 2016

4 Initializing instances

Using initialize methods can make objects usage in safer manner..
class Account
        def ac_deposit(d)
                @balance=10000
                @balance=@balance+d
        end
        def withdraw(a)
                @balance-=a
        end

        def show_bal
                puts "\tCurrent balance: #{@balance}"
        end
end

currentAC=Account.new
amt=200
puts "\ndeposit #{amt}"
currentAC.ac_deposit(amt)
currentAC.show_bal

amt=2000
puts "\nWithdraw #{amt}"
currentAC.withdraw(amt)
currentAC.show_bal
In Ruby Class object instance

Tuesday, 22 November 2016

Object-oriented Ruby : Inheritance

This is first time I've experiencing Object-orient scripting language. Enjoying inheritance parent-child relationships in the programming. It can be defined as sharing methods between classes. it avoids copynjing and pasting of same kind of methods and variables. Inheritance has two concepts which are Super class and subclass. Let me share my Example:
     class Vechile
         def accelrate
           puts "floor it"
         end

         def horn
           puts "blow blow"
         end
      end

     class Car < Vechile
     end

 vechile=Car.new
 vechile.accelrate
 vechile.horn
I've issue with my Linux box so I've installed on Windows.
C:\Users\JITTA>ruby -version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32]
-e:1:in `
': undefined local variable or method `rsion' for main:Object (NameError)
Let's start irb interactive mode:
C:\Users\JITTA>irb -I
irb(main):001:0> class Vechile
irb(main):002:1>          def accelrate
irb(main):003:2>            puts "floor it"
irb(main):004:2>          end
irb(main):005:1>
irb(main):006:1*          def horn
irb(main):007:2>            puts "blow blow"
irb(main):008:2>          end
irb(main):009:1>       end
=> :horn
irb(main):010:0>
irb(main):011:0*      class Car < Vechile
irb(main):012:1>      end
=> nil
irb(main):013:0>
irb(main):014:0*  vechile=Car.new
=> #
irb(main):015:0>  vechile.accelrate
floor it
=> nil
irb(main):016:0>  vechile.horn
blow blow
=> nil
irb(main):017:0>
The "super" keyword is used to get the parent attributes into derived class.
irb(main):001:0> class Person
irb(main):002:1>   def greeting
irb(main):003:2>     puts "hello"
irb(main):004:2>    end
irb(main):005:1> end
=> :greeting
irb(main):006:0> class Friend < Person
irb(main):007:1>    def greeting
irb(main):008:2>    super
irb(main):009:2>     puts "glad to see you!"
irb(main):010:2>     end
irb(main):011:1> end
=> :greeting
irb(main):012:0> Friend.new.greeting
hello
glad to see you!
Without Super keyword
irb(main):013:0>  class Person
irb(main):014:1>    def greeting
irb(main):015:2>      puts "hello"
irb(main):016:2>     end
irb(main):017:1> end
=> :greeting
irb(main):018:0> class Friend < Person
irb(main):019:1>   def greeting
irb(main):020:2>     puts "glad to see you"
irb(main):021:2>    end
irb(main):022:1> end
=> :greeting
irb(main):023:0> Friend.new.greeting
glad to see you
=> nil
irb(main):024:0>
The Object class The uncreated super class of a class untill it is created as subclass is said to be Object class Example:
           class Unix       /*Object class*/
              def kernal
                  puts "The unix kernal is  different to linux"
               end
               def vendor

                  puts "Unix has diffrent vendors like ibm,hp and oracle"
               end
          end
          class Solaris <  Unix   /*Super class*/
                 end
           os=Solaris.new
           os.kernal
           os.vendor
     
One more experiment with the inheritance
irb(main):001:0>
irb(main):002:0*            class Unix
irb(main):003:1>               def kernal
irb(main):004:2>                   puts "The unix kernal is  different to linux"
irb(main):005:2>                end
irb(main):006:1>                def vendor
irb(main):007:2>
irb(main):008:2*                   puts "Unix has diffrent vendors like ibm,hp and oracle"
irb(main):009:2>                end
irb(main):010:1>           end
=> :vendor
irb(main):011:0>           class Solaris <  Unix
irb(main):012:1>                  end
=> nil
irb(main):013:0>            os=Solaris.new
=> #
irb(main):014:0>            os.kernal
The unix kernal is  different to linux
=> nil
irb(main):015:0>            os.vendor
Unix has diffrent vendors like ibm,hp and oracle
=> nil
In Ruby script why everything inherit from the Object class? Because the Object class provides useful methods which needed by every Ruby object like
 1 to_s       /*converts an object to a string for printing   /
 2 inspect
 3 class
 4 methods
 5 instance_variables

Thursday, 17 November 2016

Ruby classes methods

Methods in Ruby:

Ruby methods are used to bundle one or more repeatable statements into single unit

Syntax:

    def methodname

        statement1
        statement 2
         ,,,,,,,,,,,,,,,,,

   end


Example:

def Systeminfo(a,b,c)
  puts "The ram size is #{a}"
  puts "The hard disk info  is  #{b}"
  puts "The OS type is #{c}"
end

Systeminfo("4GB","400GB","Centos")

Output:



Class

A class is blueprint for  making objects.When we use a class to make an object, the class describes what object knows about itself, as well as what object does.

Ruby Object

An object is an instance which has set  of  data and methods which operate on that data  in one place

Advantages of Class


  1. Puts data at single unit
  2. Reduces the complexity
  3. Makes programmer easier


Syntax:

 class Classname
   def mehtod1
     statement1
     statement2
     ,,,,,,,,,,,,,,,n
   end
   def metod2
     statement1
     statement2
     ,,,,,,,,,,,,,,,,n
   end
end


Example:

   class Account
     def ac_deposit(d)
       @balance=10000
        @balance=@balance+d
      end
      def withdraw(a)
        @balance=a
      end

     def show_bal
        puts "Current balance: #{@balance}"
     end
end

currentAC=Account.new
currentAC.ac_deposit(500)
currentAC.show_bal
currentAC.withdraw(2000)
currentAC.show_bal