Ruby: Difference between revisions
No edit summary |
|||
Line 13: | Line 13: | ||
* _ Provides the value of the last evaluated expression | * _ Provides the value of the last evaluated expression | ||
* Up arrow to go through history | * Up arrow to go through history | ||
= | =Data Types= | ||
The following are the built in data types | The following are the built in data types | ||
* Booleans | * Booleans | ||
Line 25: | Line 25: | ||
Boolean data type represents only one bit of information either true or false. | Boolean data type represents only one bit of information either true or false. | ||
==Numbers== | ==Numbers== | ||
'''Fixnum''' - Normal number e.g. 1 | '''Fixnum''' - Normal number e.g. 1<br> | ||
'''Bignum''' - Big numbers e.g. 111111111111 | '''Bignum''' - Big numbers e.g. 111111111111<br> | ||
'''Float''' - Decimal number e.g. 3.142 | '''Float''' - Decimal number e.g. 3.142<br> | ||
'''Complex''' - Imaginary number e.g. 4 + 3i | '''Complex''' - Imaginary number e.g. 4 + 3i<br> | ||
'''Rational''' - Fractional number e.g. 9/4 | '''Rational''' - Fractional number e.g. 9/4<br> | ||
'''BigDecimal''' - Precision number e.g. 6.0 | '''BigDecimal''' - Precision number e.g. 6.0<br> | ||
==Strings== | ==Strings== |
Revision as of 01:28, 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
- Booleans
- 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"]