Ruby programming and its basic conventions
Introduction: Ruby programming
Ruby is an object oriented programming language. Everything in ruby is taken as object. OO concepts basically includes two major things:
Class and Objects. Class is nothing but an object which consists of variables or state and methods.
State stores some value and methods perform action of some kind like print, delete etc. Objects or instance of class can be created by
using new()
method.
For e.g :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Animal
@play
def eat
puts "Animal eats" #Animal eats
end
def move(a)
puts "#{a} moves"
end
end
animal1 = Animal.new #Creates an instance of class Animal
animal1.eat #Animal eats
animal1.move("Cat") #Cat moves
Here, Animal
is a class with instance variable @play
and methods eat and move(a)
. Among them,eat
method doesnot take any parameters
whereas move(a)
does. Here, the parameter is a
.
animal1
is an instance of class Animal
so it can use all the variables and methods defined in class Animal
.
So, that was the basic introduction on Ruby. Let’s move to some of its conventions.
Ruby Conventions
Here I will be talking about some of the most important conventions in Ruby.
Class names, module names, and constants must start with an uppercase letter. By convention they use capitalization to distinguish the start of words within the name. Eg: Animal, PurchaseOrder, StudentSubject etc.
Local variables, method parameters, and method names should all start with a lowercase letter or with an underscore: order , _item , and xr2000 are all valid.
Global variables start with
$
like$name
and class variables start with@@
like@@name
.Use underscore to separate words in a multiword method or variable name. Eg:
full_name
,std_id
, etc.Constants start with uppercase letter like
Pi
,Radius
.
Reserved Words
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
BEGIN Code, enclosed in { and }, to run before the program runs.
END Code, enclosed in { and }, to run when the program ends.
alias Creates an alias for an existing method, operator, or global variable.
and Logical operator; same as && except and has lower precedence.
begin Begins a code block or group of statements; closes with end.
break Terminates a while or until loop or a method inside a block.
\case Compares an expression with a matching when clause; closes with end.
class Defines a class; closes with end.
def Defines a method; closes with end.
defined? Determines if a variable, method, super method, or block exists.
do Begins a block and executes code in that block; closes with end.
else Executes if previous conditional, in if, elsif, unless, or when, is not true.
elsif Executes if previous conditional, in if or elsif, is not true.
end Ends a code block (group of statements) starting with begin, def, do, if, etc.
ensure Always executes at block termination; use after last rescue.
false Logical or Boolean false, instance of FalseClass. (See true.)
for Begins a for loop; used with in.
if Executes code block if true. Closes with end.
module Defines a module; closes with end.
next Jumps before a loop’s conditional.
nil Empty, uninitialized variable, or invalid, but not the same as zero; object of NilClass.
not Logical operator; same as !.
or Logical operator;
redo Jumps after a loop’s conditional.
rescue Evaluates an expression after an exception is raised; used before ensure.
retry Repeats a method call outside of rescue; jumps to top of block (begin) if inside rescue.
return Returns a value from a method or block. May be omitted.
self Current object (invoked by a method).
super Calls method of the same name in the superclass. The superclass is the parent of this class.
then A continuation for if, unless, and when. May be omitted.
true Logical or Boolean true, instance of TrueClass.
undef Makes a method in current class undefined.
unless Executes code block if conditional statement is false.
until Executes code block while conditional statement is false.
when Starts a clause (one or more) under case.
while Executes code while the conditional statement is true.
yield Executes the block passed to the method.
_ FILE _ Name of current source file.
_ LINE _ Number of current line in the current source file.
Summary
Ruby Programming is an Object-Oriented Programming Language which has simple conventions and easy-to-use keywords. Following conventions,
it is easier to make software projects more readable and comprehensible. Besides, it helps to make project maintainable.
That’s all for today. See you next time.