Ruby: Difference between revisions
Line 76: | Line 76: | ||
'''.eql?''' True if the receiver and argument have both the same type and equal values.e.g. 1 == 1.0 returns true, but 1.eql?(1.0) is false.<br> | '''.eql?''' True if the receiver and argument have both the same type and equal values.e.g. 1 == 1.0 returns true, but 1.eql?(1.0) is false.<br> | ||
'''.equal?'''True if the receiver and argument have the same object id. e.g if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true. <br> | '''.equal?'''True if the receiver and argument have the same object id. e.g if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true. <br> | ||
==Assignment Operators== | |||
These are =, +=, -=, *=. /=, %= and **=. None of which are surprising in how they work. There is a parallel operators as well which swaps variables | |||
a, b = b, c |
Revision as of 01:56, 11 August 2020
Introduction
- Thoroughly object oriented
- Dynamic typing (Type errors are reported at runtime)
- Duck typing (Code requires that an object supports the operations that are used)
- Multi-paradigm (Supports procedural, functional and generic approaches)
- Reflection (Like RTTI in C++)
- Meta programming (Manipulating the underlying mechanics of the language)
- Byte code Interpreted (Like java but not like C++)
Interactive Shell
You can start this using. You can type exit to well exit
irb
This allows you to work with ruby similar to python.
- _ Provides the value of the last evaluated expression
- Up arrow to go through history
Data Types
The following are the built in data types
- Boolean
- Numbers
- Strings
- Symbols
- Hashes
- Arrays
Boolean
Boolean data type represents only one bit of information either true or false.
Numbers
Fixnum - Normal number e.g. 1
Bignum - Big numbers e.g. 111111111111
Float - Decimal number e.g. 3.142
Complex - Imaginary number e.g. 4 + 3i
Rational - Fractional number e.g. 9/4
BigDecimal - Precision number e.g. 6.0
Strings
A string is a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within single (') or double (") quote.
Symbols
Symbols are like strings. A symbol is preceded by a colon (:). For example,
:abcd
They do not contain spaces. Symbols containing multiple words are written with (_). One difference between string and symbol is that, if text is a data then it is a string but if it is a code it is a symbol.
Symbols are unique identifiers and represent static values, while string represent values that change.
Hash
A hash assign its values to its keys. They can be looked up by their keys. Value to a key is assigned by => sign. A key/value pair is separated with a comma between them and all the pairs are enclosed within curly braces. For example,
{"Akash" => "Physics", "Ankit" => "Chemistry", "Aman" => "Maths"}
Arrays
An array stroes data or list of data. It can contain all types of data. Data in an array are separated by comma in between them and are enclosed by square bracket. For example,
["Akash", "Ankit", "Aman"]
Operators
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus i.e. returns remainder
** Exponent e.g. 10**20 is 10 to the power of 20
Comparison Operators
== Check if values are equal
!= Check if values are not equal
> Check if value of left greater than value on right
< Check if value of left less than value on right
>= Check if value of left greater than or equal to value on right
<= Check if value of left less than or equal to value on right
<=> Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second.
=== Used to test equality within a when clause of a case statement. e.g. (1...10) === 5 returns true.
.eql? True if the receiver and argument have both the same type and equal values.e.g. 1 == 1.0 returns true, but 1.eql?(1.0) is false.
.equal?True if the receiver and argument have the same object id. e.g if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true.
Assignment Operators
These are =, +=, -=, *=. /=, %= and **=. None of which are surprising in how they work. There is a parallel operators as well which swaps variables
a, b = b, c