0% found this document useful (0 votes)
76 views3 pages

Ruby in Twenty Minutes: Download

This document provides a 20-minute tutorial on Ruby basics. It introduces interactive Ruby (IRB), shows how to do simple math operations and variable assignments, and explains how to call methods like sqrt. Modules are described as a way to group similar methods under familiar names, like the Math module for mathematical functions. The tutorial concludes by returning to a more traditional "Hello World" message example.

Uploaded by

Swapnil Patil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views3 pages

Ruby in Twenty Minutes: Download

This document provides a 20-minute tutorial on Ruby basics. It introduces interactive Ruby (IRB), shows how to do simple math operations and variable assignments, and explains how to call methods like sqrt. Modules are described as a way to group similar methods under familiar names, like the Math module for mathematical functions. The tutorial concludes by returning to a more traditional "Hello World" message example.

Uploaded by

Swapnil Patil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

10/12/12

Ruby in Twenty Minutes

Ruby in Twenty Minutes


Introduction
This is a small Ruby tutorial that should take no more than 20 minutes to complete. It makes the assumption that you already have Ruby installed. (If you dont have Ruby on your computer download and install it before you get started.)

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 >

Ok, so its open. Now what? Type this:


" H e l l oW o r l d "

i r b ( m a i n ) : 0 0 1 : 0 >" H e l l oW o r l d " = >" H e l l oW o r l d "

Ruby Obeyed You!


What just happened? Did we just write the worlds shortest Hello World program? Not exactly. The second line is just IRBs way of telling us the result of the last expression it evaluated. If we want to print out Hello World we need a bit more:
i r b ( m a i n ) : 0 0 2 : 0 >p u t s" H e l l oW o r l d " H e l l oW o r l d = >n i l p u t s is

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

Ruby in Twenty Minutes

Your Free Calculator is Here


Already, we have enough to use IRB as a basic calculator:
i r b ( m a i n ) : 0 0 3 : 0 >3 + 2 = >5

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

Next, lets try three squared:


i r b ( m a i n ) : 0 0 5 : 0 >3 * * 2 = >9

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 ?

Modules, Group Code by Topic


M a t h is

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

Ruby in Twenty Minutes

website.

www.ruby -lang.org/en/documentation/quickstart/

3/3

You might also like