Ruby Syntax


Let us write a simple program in ruby. All ruby files will have extension .rb. So put the following source code in a test.rb file.

      puts "Hello, Ruby!"
      

Here I assumed that you have ruby interpreter available and the "C:\ruby\bin" directory is in your PATH system variable. Now try to run this program as follows:

      C:/>ruby test.rb
      

Or, if you are using SciTE, just press F5!

Any of them will produce following result:

      Hello, Ruby!
      

This was your first and simpler Ruby program, now lets see few basic concepts related to Ruby Syntax:


Whitespace in Ruby Program:

Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in strings. Sometimes, however, they are used to interpret ambiguous statements. Interpretations of this sort produce warnings when the -w option is enabled.

Example:

      a + b is interpreted as a+b ( Here a is a local variable)
      a  +b is interpreted as a(+b) ( Here a is a method call)
      

Line Endings in Ruby Program:

Ruby interprets semicolons and newline characters as the ending of a statement. However, if Ruby encounters operators, such as +, -, or backslash at the end of a line, they indicate the continuation of a statement.

Ruby Identifiers:

Identifiers are names of variables, constants, and methods. Ruby identifiers are case sensitive. It means Ram and RAM are two different idendifiers in Ruby.

Ruby identifier names may consist of alphanumeric characters and the underscore character ( _ ). There are a few more valid characters, but that is an advanced topic.


Here Document in Ruby:

"Here Document" or HereDoc refers to build strings from multiple lines. Following a << you can specify a string or an identifier to terminate the string literal, and all lines following 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 literal. Notice there must be no space between << and the terminator.

Here are different examples:

      print <<EOF
          This is the first way of creating
          here document ie. multiple line string.
      EOF
      
      print <<"EOF";                # same as above
          This is the second way of creating
          here document ie. multiple line string.
      EOF
      
      print <<`EOC`                 # execute commands
      	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 following result:

          This is the first way of creating
          here document ie. multiple line string.
          This is the second way of creating
          here document ie. multiple line string.
      hi there
      lo there
              I said foo.
              I said bar.
      

Notice: In HereDoc, the ending delimiter must be on the left margin unless <<-EOF (specifically the dash) is used.


Ruby BEGIN Statement

Syntax:

      BEGIN {
         code
      }
      

Declares code to be called before the program is run.

Example:

      puts "This is main Ruby Program"
      
      BEGIN {
         puts "Initializing Ruby Program"
      }
      

This will produce following result:

      Initializing Ruby Program
      This is main Ruby Program
      

Ruby END Statement

Syntax:

      END {
         code
      }
      

Declares code to be called at the end of the program.

Example:

      puts "This is main Ruby Program"
      
      END {
         puts "Terminating Ruby Program"
      }
      BEGIN {
         puts "Initializing Ruby Program"
      }
      

This will produce following result:

      Initializing Ruby Program
      This is main Ruby Program
      Terminating Ruby Program
      

Ruby Comments:

Comments are lines of annotation within Ruby code. They are hidden from the Ruby interpreter and ignored at runtime. A single line comment starts with the hash # character and they extend from the # to the end of the line as follows:

      # I am a comment. Just ignore me.
      

Or, a comment may be on the same line after a statement or expression:

      name = "Madisetti" # This is again a comment
      

You can comment multiple lines as follows:

      # This is a comment.
      # This is a comment, too.
      # This is also a comment.
      # I have already said that.
      

Here is another form. Block comment conceals several lines from the interpreter using =begin and =end syntax as follows:

      =begin
      All these lines...
      Are to be considered...
      A Block comment...
      But again,I have already said that.
      =end