Let’s write a simple Ruby program. All Ruby file extensions are Here, assume that your This will produce the following results: Now that you’ve seen a simple Ruby program, let’s look at some basic concepts related to Ruby syntax: White space characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in a string. However, sometimes they are used to explain ambiguous sentences. When enabled Example: Ruby interprets semicolons and newline characters as the end of a statement.However, if Ruby encounters operators at the end of the line, such as +, -,or backslash, they represent the continuation of a statement. Identifiers are the names of variables, constants, and methods. Ruby identifiers are case sensitive. This means that Ram and RAM are two different identifiers in Ruby. The name of an 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 the names of constants or variables. 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 Yield Def In Self __FILE__ Defined? Module Super __LINE__ “Here Document” means to create a multiline string. After <<, you can specify a string or identifier to terminate the string, and all lines after the current line until the Terminator are the values of the string. If the Terminator is enclosed in quotation marks, the type of quotation marks determines the type of line-oriented string. Note that there must be no space between << and the Terminator. Here are different examples: This will produce the following results: Statement This will produce the following results: Statement This will produce the following results: Comments hide one line, or part of a line, or several lines from the Ruby interpreter. You can use the character (#) at the beginning of the line: Alternatively, comments can follow the same line of a statement or expression: You can comment on multiple lines, as follows: Here is another form. This kind of block comment is hidden from the interpreter
.rb
. so, put the following source code in
test.rb
in the file. 6.9.1. Example #
#!/usr/bin/ruby -wputs"Hello, Ruby!";
/usr/bin
Ruby interpreter is already availablein the directory. Now, try to run the program, as follows:$ ruby test.rb
Hello, Ruby!
Blanks in Ruby programs #
-w
option, this interpretation generates a warning.a+b is interpreted as a+b (this is a local variable)
a+b is interpreted as a (+b) (this is a method call)
The end of a line in a Ruby program #
Ruby identifier #
Reserved word #
Here Document in Ruby #
6.9.2. Example #
#/ Usr/bin/ruby w # - * - coding: utf-8
-*-Print<<EOF This is the first way to create a heredocument.
Multiple line strings. EOFprint<<"EOF"# Same as above, this is the second way to create a heredocument.
Multiple line strings. EOFprint<< ` EOC ` # Execute the command echoitherecholothereEOCprint<"foo",
<<"bar" # You can stack them Isaidfoo.fooIsaidbar.bar
This is the first way to create a here document.
Multiple line strings.
This is the second way to create a here document.
Multiple line strings.
Hi there
Lo there
I said foo
I say bar
Ruby BEGIN statement #
6.9.3. Grammar #
BEGIN {
code
}
code
Will be called before the program runs. 6.9.4. Example #
#/ Usr/bin/rubyputs "This is the main Ruby program"
BEGIN {puts "Initialize Ruby program"}
Initialize Ruby program
This is the main Ruby program
Ruby END statement #
6.9.5. Grammar #
END {
code
}
code
will be called at the end of the program. 6.9.6. Example #
#/ Usr/bin/rubyputs "This is the main Ruby program" END {puts "to stop Ruby
Program '{BEGIN {puts' initializes Ruby programs'}
Initialize Ruby program
This is the main Ruby program
Stop Ruby program
Ruby comment #
#I am a comment, please ignore me.
name="Madisetti"#This is also a comment
#This is a comment# This is also a comment#
This is also a comment# This is still a comment.
=begin/=end
the line between:=Begin, this is the annotation. This is also a comment.
This is also a comment. This is still a comment= End