Ruby in Twenty Minutes: Download
Ruby in Twenty Minutes: Download
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. Open up IRB (which stands for Interactive Ruby). If youre using Mac OS X open up T e r m i n a l and type i r b , then hit enter. If youre using Linux, open up a shell and type i r b and hit enter. If youre using Windows, open f x r i from the Ruby section of your Start Menu.
i r b ( m a i n ) : 0 0 1 : 0 >
the basic command to print something out in Ruby. But then whats the = >n i l bit? Thats the result of the expression. p u t s always returns nil, which is Rubys absolutely-positively-nothing value.
www.ruby -lang.org/en/documentation/quickstart/
1/3
10/12/12
Three plus two. Easy enough. What about three times two? You could type it in, its short enough, but you may also be able to go up and change what you just entered. Try hitting the up-arrow on your keyboard and see if it brings up the line with 3 + 2 on it. If it does, you can use the left arrow key to move just after the + sign and then use backspace to change it to a * sign.
i r b ( m a i n ) : 0 0 4 : 0 >3 * 2 = >6
In Ruby * * is the way you say to the power of. But what if you want to go the other way and find the square root of something?
i r b ( m a i n ) : 0 0 6 : 0 >M a t h . s q r t ( 9 ) = >3 . 0
Ok, wait, what was that last one? If you guessed, it was figuring out the square root of nine, youre right. But lets take a closer look at things. First of all, whats M a t h ?
a built-in module for mathematics. Modules serve two roles in Ruby. This shows one role: grouping similar methods together under a familiar name. M a t h also contains methods like s i n ( ) and t a n ( ) . Next is a dot. What does the dot do? The dot is how you identify the receiver of a message. Whats the message? In this case its s q r t ( 9 ) , which means call the method s q r t , shorthand for square root with the parameter of 9 . The result of this method call is the value 3 . 0 . You might notice its not just 3 . Thats because most of the time the square root of a number wont be an integer, so the method always returns a floating-point number. What if we want to remember the result of some of this math? Assign the result to a variable.
i r b ( m a i n ) : 0 0 7 : 0 >a=3* *2 = >9 i r b ( m a i n ) : 0 0 8 : 0 >b=4* *2 = >1 6 i r b ( m a i n ) : 0 0 9 : 0 >M a t h . s q r t ( a + b )= >5 . 0
As great as this is for a calculator, were getting away from the traditional beginning tutorials are supposed to focus on so lets go back to that.
H e l l oW o r l d message
that
Content available in English, French, Japanese, Korean, Polish, Spanish, Portuguese, Simplified Chinese, Traditional Chinese, Bahasa Indonesia, German, Italian, Czech, Bulgarian and Turkish. This website is made with Ruby and powered by Radiant CMS. It is proudly maintained by members of the Ruby community. Please contact our webmaster for questions or comments concerning this
www.ruby -lang.org/en/documentation/quickstart/ 2/3
10/12/12
website.
www.ruby -lang.org/en/documentation/quickstart/
3/3