Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Syntax

Ruby Syntax

shape Introduction

This chapter demonstrates about the Ruby Syntax, explains sample program, running the program and also about few editors, following are the concepts covered in this chapter.
  • Syntax Of Ruby

Syntax Of Ruby

shape Description

In order to define the syntax of the ruby create and run the program, save the program file name with .rb extension which indicates that it is a ruby file the code below demonstrates the sample code of the ruby. User need to save the program with file name.rb like hello.rb which should be saved in a local drive in a given work location then user need to run the above code in command window by passing the commands. In order to run the program user need to use the command as ruby filename.rb  as shown in below image. [ruby] puts "Hello, Ruby!"; [/ruby] Result By running the above code in command prompt user can get the following output as shown in below image. To create ruby programs user can make use of few text editors like notepad++, edit plus or can also download the Ruby Mine to create and run the program. Ruby mine doesn't require any commands to run the program and can be easily run the program, the image below demonstrates the ruby mine as shown below. White spaces are generally ignored by the ruby except in the case of strings some times which are used to interpret some ambiguous statements. Ruby BEGIN Statement Which is used to declare the code to be called before running the program the code below demonstrates the BEGIN Statement as shown below. [ruby] puts "Which is main Ruby Program" BEGIN { puts "Initializing Ruby Program" } [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. Ruby END Statement Which is used to declare the code that specified at the end of program execution, the snippet code below demonstrates the END Statement as shown below. [ruby] puts "Which is main Ruby Program" END { puts "Terminating Ruby Program" } BEGIN { puts "Initializing Ruby Program" } [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. Ruby have some list of words these are the some words kept for ruby and which are not used as constant or variable names and these are also used as method names as follows.
BEGIN END alias
do else elseif
next nil not
then true undef
and begin break
end ensure false
or redo rescue
unless until when
case class def
for if in
retry return self
yield _FILE_ _LINE_

Summary

shape Key Points

  • In ruby reserved words can be used as methods also.
  • User need to save file name with .rb extension,
  • User can use text editors to write the programs.