|
Ruby similar to other programming languages follows a set of semantics and syntax practises to make programming more professional and readable.
We will have a look on few of them one by one in this section below:
1. Naming
In ruby we always mention the class names and module names with an upper case letter .It means that the classes and modules should always start with the capital letter.
For example:
module Printable
module DeliveryDate
class StringInputMethod
class StringReplace
class PMB
Method names start with a lower case letter
def cbrt
def abp
def puts
2. Iterators
We should always use, braces ({…}) for a single line of code blocks
8.times {j| puts j}
We should use, do…end for situations where we need to put multiple lines in iteration
8.times do |j|
puts j
puts Math.sqr(j)
end
3. Parentheses And Indentation
Parentheses: It is strictly recommended to put the regular expressions and arithmetic expressions always in the parentheses as it avoids lot of confusion and errors while writing the code and thus helps to avoid the syntax errors.
puts ((7-5) * 2))
This will give the output as 4 which is the actual expected answer to this expression
puts (7+5) * 4
This will not give the expected result as the output but will display an undefined method for nil (NameError) as it doesn’t accept * 4 as part of the parameter list since the parentheses were missing in this statement.
Indentation: In ruby it is advised to start writing the code with extreme left with no indents. Two spaces are left where we have to put a Sub-indent.
4. Identifiers
Identifiers:Identifier is the term given to all the various alphabets, decimal digits along with the underscore characters that are used to represent the identity of the variables, constants etc. Each identifier starts either with the alphabetic character or the underscore. The length of the identifiers in Rubya re not restricted to any specific length.
Examples:
World
ruby_is_a_fun_to_work
5. Variables
Variables:These are again the identifiers which are used in Ruby programming to give names or identification to the values we access while programming. These Variables help us to differentiate one unique value from the other.The Vraiables can be either the local variables, Global Variables or Instance Variables. The Vraiable name in Ruby can go to any length but it is recommended to always keep the variable names short and meaningful as it makes easy to remember them while programming.
Global Variables
The variable name which starts with the $ sign is a global variable which means that it can be accessed from any anywhere in the program.Their scope remains alive till the program is alive and is in execution.
Examples:
$school
$/
Instance variables
Instance Variables are again the identifiers which always begin with the "@" character. This symbol denotes that this variable is an instance of itself.The variables which are not initialized have NIL value.
Examples:
@school
@city
6. Constants
Constants: The Identifiers in Ruby which are written in upper case letters or capital letters represent the constants in Ruby. These are defined in the class defining section of the program i.e we declare the constants at the time of defining the class. The constants should always have a assigning value and they can never be left Nil unlike Variables.
Examples:
CITY
SCHOOL
|