English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Let's write a simple Ruby program. All Ruby files have the extension .rb. So, place the following source code in the test.rb file.
#!/usr/bin/ruby -w puts "Hello, Ruby!";
Here, assume that your /usr/There is already an available Ruby interpreter in the bin directory. Now, try running this program as follows:
$ ruby test.rb
This will produce the following result:
Hello, Ruby!
You have already seen a simple Ruby program; now let's look at some basic concepts related to Ruby syntax:
Whitespace characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in strings, in which case they are not ignored. However, sometimes they are used to clarify ambiguous statements. When enabled -a warning will be generated when the w option is used.
Example:
a + b is interpreted as a+b (This is a local variable) a +b is interpreted as a(b)+b) (This is a method call)
Ruby interprets the semicolon and newline as the end of a statement. However, if Ruby encounters an operator at the end of a line, such as +、- or backslash, which indicates the continuation of a statement.
An identifier is the name of a variable, constant, or method. Ruby identifiers are case-sensitive. This means Ram and RAM are two different identifiers in Ruby.
The name of a Ruby identifier can contain letters, numbers, and underscore characters (_).
The following table lists the reserved words in Ruby. These reserved words cannot be used as constant or variable names. However, they can be used as method names.
BEGIN | do | next | then |
END | else | nil | true |
alias | elsif | not | undef |
and | end | or | unless |
begin | ensure | redo | until |
break | false | rescue | when |
case | for | retry | while |
class | if | return | while |
def | in | self | __FILE__ |
defined? | module | super | __LINE__ |
"Here Document" refers to the creation of multi-line strings. After <<, you can specify a string or identifier to terminate the string, and all lines from the current line to the terminator are the value of the string.
If the terminator is enclosed in quotes, the type of quotes determines the line-oriented string type. Please note that there should be no space between << and the terminator.
Below are different examples:
#!/usr/bin/ruby -w # -*- coding : utf-8 -*- print <<EOF This is the first way to create a here document. Multi-line string. EOF print <<"EOF"; # Same as above This is the second way to create a here document. Multi-line string. EOF print <<`EOC` # Execute command echo hi there echo lo there EOC print <<"foo", <<"bar" # You can stack them I said foo. foo I said bar. bar
This will produce the following result:
This is the first way to create a here document. Multi-line string. This is the second way to create a here document. Multi-line string. hi there lo there I said foo. I said bar.
BEGIN { code }
Declaration code It will be called before the program runs.
#!/usr/bin/ruby puts "This is the main Ruby program" BEGIN { puts "Initialize Ruby Program" }
This will produce the following result:
Initialize Ruby Program This is the main Ruby program
END { code }
Declaration code It will be called at the end of the program.
#!/usr/bin/ruby puts "This is the main Ruby program" END { puts "Stop Ruby Program" } BEGIN { puts "Initialize Ruby Program" }
This will produce the following result:
Initialize Ruby Program This is the main Ruby program Stop Ruby Program
Comments hide a line, or part of a line, or several lines from the Ruby interpreter. You can use the character (#) at the beginning of a line:
# I am a comment, please ignore me.
Or, comments can follow the same line as the statement or expression:
name = "Madisetti" # This is also a comment
You can comment multiple lines, as shown below:
# This is a comment. # This is also a comment. # This is also a comment. # This is still a comment.
Below is another form. This block comment hides =begin from the interpreter/Lines between =end:
=begin This is a comment. This is also a comment. This is also a comment. This is still a comment. =end