Ruby

From bibbleWiki
Jump to navigation Jump to search

Introduction

  • Thoroughly object oriented
  • Dynamic typing (Type errors are reported at runtime)
  • Duck typing (Code requires that an object supports the operations that are used)
  • Multi-paradigm (Supports procedural, functional and generic approaches)
  • Reflection (Like RTTI in C++)
  • Meta programming (Manipulating the underlying mechanics of the language)
  • Byte code Interpreted (Like java but not like C++)

Interactive Shell

You can start this using. You can type exit to well exit

irb

This allows you to work with ruby similar to python.

  • _ Provides the value of the last evaluated expression
  • Up arrow to go through history

Data Types

The following are the built in data types

  • Boolean
  • Numbers
  • Strings
  • Symbols
  • Hashes
  • Arrays

Boolean

Boolean data type represents only one bit of information either true or false.

Numbers

Fixnum - Normal number e.g. 1
Bignum - Big numbers e.g. 111111111111
Float - Decimal number e.g. 3.142
Complex - Imaginary number e.g. 4 + 3i
Rational - Fractional number e.g. 9/4
BigDecimal - Precision number e.g. 6.0

Strings

A string is a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within single (') or double (") quote.

Symbols

Symbols are like strings. A symbol is preceded by a colon (:). For example,

 :abcd

They do not contain spaces. Symbols containing multiple words are written with (_). One difference between string and symbol is that, if text is a data then it is a string but if it is a code it is a symbol.
Symbols are unique identifiers and represent static values, while string represent values that change.

Hash

A hash assign its values to its keys. They can be looked up by their keys. Value to a key is assigned by => sign. A key/value pair is separated with a comma between them and all the pairs are enclosed within curly braces. For example,

 {"Akash" => "Physics", "Ankit" => "Chemistry", "Aman" => "Maths"}

Arrays

An array stroes data or list of data. It can contain all types of data. Data in an array are separated by comma in between them and are enclosed by square bracket. For example,

 ["Akash", "Ankit", "Aman"]

Operators

Arithmetic Operators

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus i.e. returns remainder
** Exponent e.g. 10**20 is 10 to the power of 20

Comparison Operators

== Check if values are equal
!= Check if values are not equal
> Check if value of left greater than value on right
< Check if value of left less than value on right
>= Check if value of left greater than or equal to value on right
<= Check if value of left less than or equal to value on right
<=> Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second.
=== Used to test equality within a when clause of a case statement. e.g. (1...10) === 5 returns true.
.eql? True if the receiver and argument have both the same type and equal values.e.g. 1 == 1.0 returns true, but 1.eql?(1.0) is false.
.equal?True if the receiver and argument have the same object id. e.g if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true.

Assignment Operators

These are =, +=, -=, *=. /=, %= and **=. None of which are surprising in how they work. There is a parallel operators as well which swaps variables

 a, b = b, c

Bitwise Operators

These are &. |, ^, ~, <<, >> operators which behave like most languages

Logical Operators

and, or, &&, ||, ! and not are available.

Flow Control

if else

Some examples and no surprises

if a == "abc"
   doFoo1
elseif a == "def"
   doFoo2
else
   doFoo3
end

myVar = if a == "123" then "123" else "not 123" end

Object vs Variables

In ruby variables are reference i.e.

a = "abc"
b = a;

b is a reference to a not a copy. i.e. if we go

a.upper!

b will be upper too. To get a copy you need to use the clone method. i.e. b = a.clone. Note this is not a deep copy.

Classes

Introduction

You can create a class like this

class Shapeship

  # Multiple props
  attr_accessor :prop1, :prop2

  def use_prop
     prop1 = "Hi I am local and broken" # Won't work.
     self.prop1 = "Must use self like this!"
  end

  def launch(destination)
    @destination = destination
  end

  # Function for Getter
  def destination
    @destination
  end

  # Read/Write accessor
  attr_accessor :destination

  # Readable accessor
  attr_reader :destination

  # Writable accessor
  attr_writerr :destination
end

ship = Spaceship.new
ship.launch("Earth")

Initialize

Initialize is called when a new instance is created

class Shapeship
    def initialize(name, cargo_module_cound)
        @name = name
        @cargo_hold = CargoHold.new(cargo_module_count)
        @power_level = 100
    end
end

ship = Spaceship.new("Dreadnought", 4)

Inheritance

Nothing strange here

class Probe
   def deploy(deploy_time,return_time)
      puts "Deploying"
   end
end

class MineralProbe < Probe
   def deploy(deploy_time)
      puts "Preparing sample chamber"
      super(deploy_time, Time.now + 2 * 60 * 60) # Call base function
   end
end

Class Method (static)

class Shapeship
    def self.thruster_count
        2
    end
end
==Class Variables (static)==
<syntaxhighlight lang="ruby">
class Shapeship
    @@thruster_count = 2
end

Method Visibility

Standard Methods

Two approaches you can add the private word without the # or provide a list of private functions e.g. private:foo1, foo2

class Shapeship
    def launch
        batten_hatches
    end

# private
    def batten_hatches
       put "batten_hatches"
    end

    private:batten_hatches
end

class SpritelyShapeship < Spaceship
    def initialize
        batten_hatches # Ok from derived class
    end
end

Class Methods (statics)

Different for statics oops class methods

class SpaceShip
   def self.disable_engine
     # Blah,  Blah, Blah
   end
   private :disable_engine # Does not work
   
   private_class_method:disable_engine   
end

Summary

  • public is the default
  • private mean "can't be called with an explicit receiver"
  • private_class_method is private for class methods
  • protected means "allow access for other objects of the same class"
  • private and protected are not used much

Operator Overloading

For the equals operator you can overload your class

class SpaceShip
   attr_reader :name

   def initialize(name
       @name = name
   end

   def ==(other)
     name == other.name
   end
end

Monkey Patching

Gosh this looks awful, ruby replaces the function whilst running.

class SpaceShip
   def insane
     put "insane"
   end
end

spaceShip = SpaceShip.new

class SpaceShip
   def insane
     put "insane 2"
   end
end

puts spaceShip.insane