YouTip LogoYouTip

Ruby Syntax

# Ruby Syntax Let's write a simple Ruby program. All Ruby files have the extension **.rb**. So, put the following source code in a file called test.rb. ## Example #!/usr/bin/ruby -w puts"Hello, Ruby!"; [Run Example Β»](#) Here, we assume that you have the Ruby interpreter available in your /usr/bin directory. Now, try running this program as follows: $ ruby test.rb This will produce the following result: Hello, Ruby! You have seen a simple Ruby program, now let's look at some basic concepts related to Ruby syntax: ## Whitespace in Ruby Programs Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear within strings. However, sometimes they are used to interpret ambiguous statements. When the -w option is enabled, this interpretation can produce warnings. **Example:** a + b is interpreted as a+b (where a is a local variable) a +b is interpreted as a(+b) (where a is a method call) ## Line Endings in Ruby Programs Ruby interprets semicolons and newline characters as the end of a statement. However, if Ruby encounters an operator, such as +, -, or a backslash, at the end of a line, they indicate a continuation of the statement. ## Ruby Identifiers Identifiers are names for variables, constants, and methods. Ruby identifiers are case-sensitive. This means Ram and RAM are two different identifiers in Ruby. The names of Ruby identifiers can contain letters, digits, and the underscore character ( _ ). ## Reserved Words The following table lists all 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 yield def in self __FILE__ defined?module super __LINE__ ## Here Document in Ruby "Here Document" is used to establish multi-line strings. After <<, you can specify a string or identifier to terminate the string, and all lines from the current line up to the terminator are the value of the string. If the terminator is quoted, the type of quotes determines the type of the line-oriented string. Note that there must be no space between << and the terminator. Here are different examples: ## Example #!/usr/bin/ruby -w# -*- coding : utf-8 -*-print<<EOF This is the first way to create here document. Multi-line string. EOF print<<"EOF"; # Same as above This is the second way to create 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 [Run Example Β»](#) This will produce the following result: This is the first way to create here document. Multi-line string. This is the second way to create here document. Multi-line string. hi there lo there I said foo. I said bar. ## Ruby _BEGIN_ Statement ### Syntax BEGIN { code } Declares that _code_ is to be invoked before the program runs. ### Example #!/usr/bin/ruby puts"This is the main Ruby program"BEGIN { puts"Initializing Ruby program" } This will produce the following result: Initializing Ruby program This is the main Ruby program ## Ruby _END_ Statement ### Syntax END { code } Declares that _code_ is to be invoked at the end of the program. ## Example #!/usr/bin/ruby puts"This is the main Ruby program"END { puts"Stopping Ruby program" } BEGIN { puts"Initializing Ruby program" } This will produce the following result: Initializing Ruby program This is the main Ruby program Stopping Ruby program ## Ruby Comments Comments hide lines, or parts of lines, or multiple 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 a statement or expression: name = "Madisetti"# This is also a comment You can comment out multiple lines, as follows: # This is a comment. # This is also a comment. # This is also a comment. # This is still a comment. Here is another form. This block comment hides lines from the interpreter between =begin and =end: =begin This is a comment. This is also a comment. This is also a comment. This is still a comment. =end
← Ruby ClassRuby Environment Variables β†’