Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Ruby Programming/Hello world

From Wikibooks, open books for an open world
<Ruby Programming

← Interactive Ruby| Strings →

The classic Hello, world! program is a good way to get started with Ruby.

Hello, world!

[edit |edit source]

Create a text file calledhello_world.rb containing the following code:

puts'Hello, world!'

Now run it at the shell prompt.

$ruby hello_world.rbHello, world!

You can also run the short "Hello, world!" program without creating a text file at all. This is called aone-liner.

$ruby -e "puts 'Hello, world!'"Hello, world!

Option-e means evaluate (Ruby code). You can run this code withirb, but the output will look slightly different.puts will print out "Hello, world!", butirb will also print out the return value ofputs — which isnil.

$irb>> puts "Hello, world!"Hello, world!=> nil

Comments

[edit |edit source]

Like Perl, Bash, Python, and C Shell, Ruby uses the hash symbol (also called Pound Sign, number sign) for comments. Everything from the hash to the end of the line is ignored when the program is run by Ruby. For example, here's ourhello_world.rb program with comments.

# My first Ruby program# On my way to Ruby fame & fortune!puts'Hello, world!'

You can append a comment to the end of a line of code, as well. Everything before the hash is treated as normal Ruby code.

puts'Hello, world!'# Print out "Hello, world!"

You can also comment several lines at a time:

=beginThis program willprint "Hello, world!".=endputs'Hello, world!'

Although block comments can start on the same line as=begin, the=end must have its own line. You cannot insert block comments in the middle of a line of code as you can in C, C++, and Java, although you can have non-comment code on the same line as the=end.

=begin This program will print 'Hello, world!'=end puts 'Hello, world!'

Executable Ruby scripts

[edit |edit source]

Typing the wordruby each time you run a Ruby script is tedious. To avoid doing this, follow the instructions below.

Unix-like operating systems

[edit |edit source]

In Unix-likeoperating systems – such as Linux, Mac OS X, and Solaris you will want to mark your Ruby scripts as executable using thechmod command. This also works with the Cygwin version of Ruby.

$chmod +x hello_world.rb

You need to do this each time you create a new Ruby script. If you rename a Ruby script, or edit an existing script, you donot need to run "chmod +x" again.

Next, add ashebang line as thevery first line of your Ruby script. The shebang line is read by the shell to determine what program to use to run the script. This line cannot be preceded by any blank lines or any leading spaces. The newhello_world.rb program – with the shebang line – looks like this:

#!/usr/bin/rubyputs'Hello world'

If yourruby executable is not in the/usr/bin directory, change the shebang line to point to the correctpath. The other common place to find theruby executable is/usr/local/bin/ruby.

The shebang line is ignored by Ruby – since the line begins with a hash, Ruby treats the line as a comment. Hence, you can still run the Ruby script on operating systems such as Windows whose shell does not support shebang lines.

Now, you can run your Ruby script without typing in the wordruby. However, for security reasons, Unix-like operating systems do not search the current directory for executables unless it happens to be listed in your PATHenvironment variable. So you need to do one of the following:

  1. Create your Ruby scripts in a directory that is already in your PATH.
  2. Add the current directory to your PATH (not recommended).
  3. Specify the directory of your script each time you run it.

Most people start with #3. Running an executable Ruby script that is located in the current directory looks like this:

$./hello_world.rb

Once you have completed a script, it's common to create a~/bin directory, add this to your PATH, and move your completed script here for running on a day-to-day basis. Then, you can run your script like this:

$hello_world.rb

Using env

[edit |edit source]

If you do not want to hard-code the path to theruby executable, you can use theenv command in the shebang line to search for theruby executable in your PATH and execute it. This way, you will not need to change the shebang line on all of your Ruby scripts if you move them to a computer with Ruby installed in a different directory.

#!/usr/bin/env rubyputs'Hello world'

Windows

[edit |edit source]

If you install the native Windows version of Ruby using theRuby One-Click Installer, then the installer has setup Windows to automatically recognize your Ruby scripts as executables. Just type the name of the script to run it.

$hello_world.rbHello world

If this does not work, or if you installed Ruby in some other way, follow these steps.

  1. Log in as anadministrator.
  2. Run the standard Windows "Command Prompt",cmd.
  3. At the command prompt (i.e. shell prompt), run the following Windows commands. When you runftype, change the command-line arguments to correctly point to where you installed theruby.exe executable on your computer.
$assoc .rb=RubyScript.rb=RubyScript$ftype RubyScript="c:\ruby\bin\ruby.exe" "%1" %*RubyScript="c:\ruby\bin\ruby.exe" "%1" %*

For more help with these commands, run "help assoc" and "help ftype".

Retrieved from "https://en.wikibooks.org/w/index.php?title=Ruby_Programming/Hello_world&oldid=4071760"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp