Movatterモバイル変換


[0]ホーム

URL:


Ruby

A Programmer's Best Friend

HomeDownloadsDocumentationLibrariesCommunityNewsSecurityAbout RubyMenu
1 |2 |3 |4

Ruby in Twenty Minutes

Introduction

This is a small Ruby tutorial that should take no more than 20 minutesto complete. It makes the assumption that you already have Ruby installed.(If you do not have Ruby on your computerinstall itbefore you get started.)

Interactive Ruby

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

Open up IRB (which stands for Interactive Ruby).

  • If you’re usingmacOS open upTerminal and typeirb, thenhit enter.
  • If you’re usingLinux, open up a shell and typeirb and hitenter.
  • If you’re usingWindows, openInteractive Ruby from theRuby section of your Start Menu.
irb(main):001:0>

Ok, so it’s open. Now what?

Type this:"Hello World"

irb(main):001:0>"Hello World"=>"Hello World"

Ruby Obeyed You!

What just happened? Did we just write the world’s shortest “Hello World”program? Not exactly. The second line is just IRB’s way of telling usthe result of the last expression it evaluated. If we want to print out“Hello World” we need a bit more:

irb(main):002:0>puts"Hello World"Hello World=>nil

puts is the basic command to print something out in Ruby. But thenwhat’s the=> nil bit? That’s the result of the expression.putsalways returns nil, which is Ruby’s absolutely-positively-nothing value.

Your Free Calculator is Here

Already, we have enough to use IRB as a basic calculator:

irb(main):003:0>3+2=>5

Three plus two. Easy enough. What about threetimes two? You couldtype it in, it’s short enough, but you may also be able to go up andchange what you just entered. Try hitting theup-arrow on yourkeyboard and see if it brings up the line with3+2 on it. If it does,you can use the left arrow key to move just after the+ sign and thenuse backspace to change it to a* sign.

irb(main):004:0>3*2=>6

Next, let’s try three squared:

irb(main):005:0>3**2=>9

In Ruby** is the way you say “to the power of”. But what if you wantto go the other way and find the square root of something?

irb(main):006:0>Math.sqrt(9)=>3.0

Ok, wait, what was that last one? If you guessed, “it was figuring outthe square root of nine,” you’re right. But let’s take a closer look atthings. First of all, what’sMath?

Modules Group Code by Topic

Math is a built-in module for mathematics. Modules serve two roles inRuby. This shows one role: grouping similar methods together under afamiliar name.Math also contains methods likesin() andtan().

Next is a dot. What does the dot do? The dot is how you identify thereceiver of a message. What’s the message? In this case it’ssqrt(9),which means call the methodsqrt, shorthand for “square root” with theparameter of9.

The result of this method call is the value3.0. You might notice it’snot just3. That’s because most of the time the square root of anumber won’t be an integer, so the method always returns afloating-point number.

What if we want to remember the result of some of this math? Assign theresult to a variable.

irb(main):007:0>a=3**2=>9irb(main):008:0>b=4**2=>16irb(main):009:0>Math.sqrt(a+b)=>5.0

As great as this is for a calculator, we’re getting away from thetraditionalHello World message that beginning tutorials are supposedto focus on…so let’s go back to that.


Participate in a friendly and growing community.

Syndicate

Recent News (RSS)


HomeDownloadsDocumentationLibrariesCommunityNewsSecurityAbout Ruby

This site in other languages:Български,Deutsch,English,Español,Français,Bahasa Indonesia,Italiano,日本語,한국어,polski,Português,Русский,Türkçe,Tiếng Việt,简体中文,繁體中文.

This website is proudly maintained by members of the Ruby community.


[8]ページ先頭

©2009-2025 Movatter.jp