Sunday, April 14, 2013

Ruby Tutorial


 Ruby Installation with graphics (painful)
==============================

* Install RVM for Ruby -
\curl -#L https://get.rvm.io | bash -s stable --autolibs=3 --ruby

* Install TK from ActiveTcl - http://www.activestate.com/activetcl

* Run
rvm reinstall 2.0.0 --enable-shared --enable-pthread --with-tk --with-tcl

Recommended Sites
==================

1. From author (Yukihiro Matsumore)

http://rubymonk.com/

2. Basic
http://www.tutorialspoint.com/ruby/ruby_variables.htm


Interactive Tutorial
==============
http://tryruby.org/

Run ruby
=========
1. Using interpreter

irb ( start interpreter)
load "fname.rb" ( load the file)
ruby "fname.rb" ( run the file)

2. Using ruby command

ruby "fname.rb" ( run the file)

3. Type of Variables & Mehod
====================


@instanceVariable  
@@classVariable
$GlobalVariable


4. instanceMethod

def fnc
...
end

5. ClassMethod
=============
def self.fnc
...
end


6. Anonymous/Block 
================
  3.times { puts "hello"}

 arr = [1,2,3,4]
 arr.each { puts "arr"}
 arr.each { |cur|  puts cur}
   i = 50
  arr.each { |cur| if i> cur then puts i else puts cur  end}
   arr.each { |cur| puts cur*2}
   arr.map { |cur| cur*2}
   arr.any? { |cur| cur> 0}
   arr.inject { |acc,cur| acc+cur}
   arr.select { |cur| cur> 3}


Sending block as parameter(NO need to capture block in callee) and run block yield
=============================================================

class Foo
  def initialize(max)
    @max = max
  end

  def silly(i)
    yield(i,5) + yield(@max,@max) # yield will execute block
  end
end

f = Foo.new(1000)
f.silly(10) {|a,b|   a + b}  # block just double values

Proc like block but returns object
=======================

c = [3,5,7,9]
 c = a.map { |x| lambda{ |y|  x>y}  }
c[0].call 33


Loop
========
 i = 5
j = 9
(0..i).each do |curI|
    print curI.to_s + "\n"
    (curI..j).each do |curJ|
        print curJ.to_s
    end
end



Dynamic dispatching
================

class Point
  attr_accessor :x, :y

  def initialize(x,y)
    @x = x
    @y = y
  end
  def distFromOrigin
    Math.sqrt(@x * @x  + @y * @y) # uses instance variables directly
  end
  def distFromOrigin2
    Math.sqrt(x * x + y * y) # uses getter methods # CALL THE GETTER METHOD FROM POLARPOINT . BECOZ IT IS A FUNCTION CALL AND DUE TO DYNAMIC DISPATCH
        # IT WILL START SEARCHING METHOD FROM SUB-CLASS
  end

end


class PolarPoint < Point
  # Interesting: by not calling super constructor, no x and y instance vars
  # In Java/C#/Smalltalk would just have unused x and y fields
  def initialize(r,theta)
    @r = r
    @theta = theta
  end
  def x
    @r * Math.cos(@theta)
  end
  def y
    @r * Math.sin(@theta)
  end
  def x= a
    b = y # avoids multiple calls to y method
    @theta = Math.atan2(b, a)
    @r = Math.sqrt(a*a + b*b)
    self
  end
  def y= b
    a = x # avoid multiple calls to y method
    @theta = Math.atan2(b, a)
    @r = Math.sqrt(a*a + b*b)
    self
  end
  def distFromOrigin # must override since inherited method does wrong thing
    @r
  end
  # inherited distFromOrigin2 already works!!
end

# the key example
pp = PolarPoint.new(4,Math::PI/4)
pp.x
pp.y
pp.distFromOrigin2