Ruby: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "= Introduction = * Thoroughly object oriented * Dynamic typing (Type errors are reported at runtime) * Duck typing (Code requires that an object supports the operations that a..."
 
No edit summary
Line 1: Line 1:
= Introduction =
=Introduction=
* Thoroughly object oriented
* Thoroughly object oriented
* Dynamic typing (Type errors are reported at runtime)
* Dynamic typing (Type errors are reported at runtime)
Line 7: Line 7:
* Meta programming (Manipulating the underlying mechanics of the language)
* Meta programming (Manipulating the underlying mechanics of the language)
* Byte code Interpreted (Like java but not like C++)
* 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
=Variables=
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.
<br>
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"]

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

Variables

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"]