Friday, September 16, 2011

Starting with Ruby!!


Interactive Ruby

Ruby comes with a program that will show the results of any Ruby statements you feed it.
Playing with Ruby code in interactive sessions like this is a terrific way to learn the
language.

To print something to the interactive ruby terminal (IRB)

Just type in something enclosed within double quotes and click enter key.
For example type: "Ruby Language"

irb(main):001:0> "Ruby Language"
=> "Ruby Language"


Actually the second line is just IRB’s way of telling us the result of the last expression it
evaluated.

So to print out "Ruby Language" we need a bit more.

irb(main):002:0> puts "Ruby Language"
"Ruby Language"
=> nil 

puts is the basic command in Ruby to print out something. Now whats this "nil"?. It is nothing but the result of expression just executed. puts command always return "nil", which is positively-nothing value.


Ruby provides calculator

To calculate something just type it and click enter key. For example to sum up 4 and 3, type in 4+3.


irb(main):003:0> 4+3
=> 7
irb(main):004:0> 4*2
=> 8

irb(main):005:0> 4**2
=> 16

irb(main):006:0> Math.sqrt(16)
=> 4.0 

** corresponds to square
Math is a built-in module for Mathematics in Ruby.
sqrt() is a method in the module Math.




0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...