Ruby is the creation of a Japanese Computer Scientist Yukihiro Matsumoto. Ruby is an open-source, interpreted programming language. It has a very elegant syntax and you can even write code which might (somewhat) resemble functional style programming code. The Ruby interpreter can be installed on Windows, Linux or OS X. RVM ( Ruby Versioning Manager) is a good way to organize, install and maintain multiple ruby versions on the same machine. How to install Ruby and download ruby are good resources to setup Ruby and get it running.
This allows you to enter ruby commands line by line and see the output immediately. That's a pretty effective way to get used to the Ruby syntax, commands and keywords quickly. Open up an OS X Terminal or a Linux Shell and type in irb. On Windows, I believe this is fxri. You now find yourself in the Interactive Ruby Shell.
1. Type in "XYZ"
The output you see => "XYZ" is the value being returned by the line or expression, after evaluation by the Ruby interpreter.
2. Type in puts "XYZ"
It displays "XYZ". The statement returns nothing, which is why you see the =>nil.
3. Simple Arithmetic expression
4. Two consecutive asterisks indicate "raise to the power of". So, 5**2 = 5^2 = 25.
5. Let's take a quick tour of 'Math' which is a built in module, which provides you methods and constants which are very useful while making mathematical calculations. 'sqrt' for instance is the way to find the square root of a given number.
6. Here's how we find the sine of an angle. Math::PI is the value of the constant PI as provided by the Math module. Sine of PI/2 radians, or 90 degrees = 1.
7. Inverse trigonometric ratios. Lets find the sine-inverse or the arcsine of 1 and 0.5
The values you see are in radians; in terms of degrees they are 90 and 30 degrees respectively.
Check out some of our other Ruby Tutorials :
Computer Science >