Skip to main content

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



Comments

Popular posts from this blog

MATLAB cross validation

// use built-in function samplesize = size( matrix , 1); c = cvpartition(samplesize,  'kfold' , k); % return the indexes on each fold ///// output in matlab console K-fold cross validation partition              N: 10    NumTestSets: 4      TrainSize: 8  7  7  8       TestSize: 2  3  3  2 ////////////////////// for i=1 : k    trainIdxs = find(training(c,i) ); %training(c,i);  // 1 means in train , 0 means in test    testInxs  = find(test(c,i)       ); % test(c,i);       // 1 means in test , 0 means in train    trainMatrix = matrix (  matrix(trainIdxs ), : );    testMatrix  = matrix (  matrix(testIdxs  ), : ); end //// now calculate performance %%  calculate performance of a partition     selectedKfoldSen=[];selectedKfoldSpe=[];selectedKfoldAcc=[];     indexSen=1;indexSpe=1;indexAcc=1;     if ( kfold == (P+N) )% leave one out         sensitivity = sum(cvtp) /( sum(cvtp) + sum(cvfn) )         specificity = sum(cvtn) /( sum(cvfp) + sum(cvtn) )         acc

R tutorial

Install R in linux ============ In CRAN home page, the latest version is not available. So, in fedora, Open the terminal yum list R  --> To check the latest available version of r yum install R --> install R version yum update R --> update current version to latest one 0 find help ============ ?exact topic name (  i.e.   ?mean ) 0.0 INSTALL 3rd party package  ==================== install.packages('mvtnorm' , dependencies = TRUE , lib='/home/alamt/myRlibrary/')   #  install new package BED file parsing (Always use read.delim it is the best) library(MASS) #library(ggplot2) dirRoot="D:/research/F5shortRNA/TestRIKEN/Rscripts/" dirData="D:/research/F5shortRNA/TestRIKEN/" setwd(dirRoot) getwd() myBed="test.bed" fnmBed=paste(dirData, myBed, sep="") # ccdsHh19.bed   tmp.bed ## Read bed use read.delim - it is the  best mybed=read.delim(fnmBed, header = FALSE, sep = "\t", quote = &q