Prerequisites for Ruby Tutorial:
Install Ruby – Here you will found How to install Ruby?
Ruby is a scripting language designed by Yukihiro Matsumoto(Matz). In my previous article Why Rails ? , I wrote –“Ruby is a genuine Object Oriented Language”, cause of which I am bit confuse whether to use top down approach for these ruby tutorial or bottom up? Let’s use bottom up, which means from basics( About variable, methods, loops etc) to classes(Object oriented methodology) . It may be helpful for procedural language programmer and beginner.
Ruby First Program
It is easy to start programming with ruby rather than other languages, the reason is that ruby uses simple syntax rules as you are defining something in English. You will see it in our below examples. Let’s start with simple Ruby Program( Ruby First Program) which will print something. Just write below line in your favorite text editor, we are using here Sublime text editor for programmers and save it as buffercode1.rb.
puts "Greeting from Buffercode"
Now just Open your terminal(For Unix) or command prompt(For Windows), change your current directory to the directory where you saved your ruby program using cd command and type
ruby buffercode1.rb
Interactive Ruby
You can use Interactive Ruby ( irb ) to test your code, follow the below step to use it.
Open your terminal and type irb , it will start your ruby shell.
Now just enter your code and it will give you output on next line, see its example
irb(main):001:0>puts "Greeting from Buffercode" Greeting from Buffercode =>nil irb(main):002:0>Time.now #it will show current time of your system
As you can see irb will give output on very next line of your code, there is no need of compilation of your code, just type and run. Irb will interact with your code that’s why it called interactive ruby. You can use irb as basic calculator too 😉
irb as basic calculator
irb(main):001:0>2+2 =>4 irb(main):002:0>Math.sqrt(9) =>3.0 irb(main):003:0>Math.sin(Math::PI/2) #To find value of sin PI/2 in ruby =>1.0
All these are ruby methods , you will learn more about them in our ruby tutorial.
Have something to add or stuck somewhere in this Ruby Tutorial?? Share it in comments .
Follow us on Facebook, Google Plus and Twitter to get updated with our ruby tutorial.