Introduction
Ruby is a pure object-oriented programming language. It was created in 1993 by Yukihiro Matsumoto of Japan.
Ruby is "A Programmer's Best Friend". Ruby has features that are similar to those of Smalltalk, Perl, and Python. Perl, Python, and Smalltalk are scripting languages.
Smalltalk is a true object-oriented language. Ruby, like Smalltalk, is a perfect object-oriented language. Using Ruby syntax is much easier than using Smalltalk syntax.
Features:
- General Purpose and Interpreted
- True Objce Oriented Programming Language
- Server-Side Scripting Language
- Clean and Easy syntax
Example:
puts 'hello ruby'
Above code prints hello ruby to the screen
Remember This
Extension:
Ruby file Extension:.rb
Whitespace
Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in strings.
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
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.
Identifiers
Identifiers are names of variables, constants, and methods.
Ruby identifiers are case sensitive.
It means Ram and RAM are two different identifiers in Ruby.
Ruby identifier names may consist of alphanumeric characters and the underscore character ( _ ).
Comment:
Single Line Comment:
# I am a comment. Just ignore me.
Multi Line Comment:
=begin
This is a comment.
This is a comment, too.
=end
Variables
Variables are the memory locations, which hold any data to be used by any program.
We are covering local variables only! See the reference at the end to know more about other types!
Local Varibales
math = 100
science = "awesome"
Literals
Integers
Ruby supports integer numbers.
An integer number can range from -230 to 230-1 or -262 to 262-1.
123
-500
Floats
Ruby supports floating numbers.
hey are also numbers but with decimals.
124.4
Strings
Ruby strings are simply sequences of 8-bit bytes and they are objects of class String.
Double-quoted strings allow substitution and backslash notation but single-quoted strings don't allow substitution and allow backslash notation only for \\ and \'
puts 'escape using "\\"';
puts 'That\'s right';
Result:
escape using "\"
That's right
Statements
if...else
- if expressions are used for conditional execution.
- The values false and nil are false, and everything else are true.
- Notice Ruby uses elsif, not else if nor elif.
x=1
if x > 2
puts "x is greater than 2"
elsif x <= 2 and x != 0
puts "x is 1"
else
puts "I can't guess the number"
end
Result:
x is 1
unless
Executes code if conditional is false. If the conditional is true, code specified in the else clause is executed.
x = 1
unless x >=2
puts "x is less than 2"
else
puts "x is greater than 2"
end
Result:
x is less than 2
Loops
while
Executes code while conditional is true. A while loop's conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;.
i = 0
num = 3
while i < num do
puts i
end
Result:
0
1
2
until
Executes code while conditional is false. An until statement's conditional is separated from code by the reserved word do, a newline, or a semicolon.
i = 0
num = 3
until i > num do
puts i
end
Result:
0
1
2
3
for
Executes code once for each element in expression.
for i in 0..2
puts i
Result:
0
1
2
This is a very basic documentation, To learn elaborately click here!
![](./images/ruby.png)