Monday, November 29, 2010

Ruby

Ruby

-----------------------------
to check the path of the ruby

which ruby

/usr/bin/ruby
-------------------------------
If you executing ruby from shell script

go to vi mode ......and then type the programme and save it in .rb  The file name should have extension of .rb and save it any where in !

vi trial.rb

#!/usr/bin/ruby

./trial.rb

--------------------------------
If you executing ruby from normal mode

vi trial.rb (assume that we save in desktop or file system )

ruby trial.rb (to compile the ruby code)
---------------------------------
comments in ruby    

single line comment

#This is the single line comment in ruby

multiple line comment

==begin

to start with multiple line comment we have to start ==begin from beggining and ==end from ending

==end
----------------------------------

declaring variables in ruby

a=10
b=10
c=10

parallel way of assigning variables in ruby

a,b,c,d=10,20,30,45
------------------------------------
to know the what type of variables in ruby

a=25
b=45.0
c="welcome"
a.class()
b.class()
c.class()

to check whether it is integer/float/string

a.kind_of? Integer
b.kind_of? String
----------------------------------------
converting variables values

    y=20
         y.to_s  #it converts to string
         y.to_f  #it converts to float

to convert the integer binary
              y.to_s(2)   #converts to binary base 2
              y.to_s(8)   #converts to base 8 (octal)
              y.to_s(16)  #converts to base 16(Hexa decimal)

--------------------------------
variable scope in ruby

ruby has 5 types of variable scope in ruby

$ --global variable
@-- instance variable
a-z--local variable
A-Z-- constant variable
@@---class variable

to know the type of the variable in ruby

defined? ($x or @x or x or @@x)