What is Ruby on Rails ?
What is Ruby ?
Numbers : Integers and floating point numbers come in the category of numbers. Integers are held internally in binary form. Integer numbers are numbers without a fraction.
Strings :
Arrays :
Hashes :
- Ruby on Rails is a server-side web application development framework written in Ruby language by David Heinemeier Hansson.
- It allows us to write less code than other languages and frameworks. It includes everything needed to create database-backed web applications according to MVC pattern.
What is Ruby ?
- Ruby is a dynamic, open source, object oriented programming language. It runs on all types of platforms like Windows, Mac OS and all versions of UNIX.
- It is fully object oriented programming language. Everything is an object in Ruby. Each and every code has their properties and actions. Here properties refer to variables and actions refer to methods.
History of Ruby :
- Ruby is designed and developed by Yukihiro "Martz" Matsumoto in mid 1990's in Japan.
- The name "Ruby" originated during a chat session between Matsumoto and Keiju Ishitsuka. Two names were selected, "Coral" and "Ruby". Matsumoto chose the later one as it was the birthstone of one of his colleagues.
- The first public release of Ruby 0.95 was announced on Japanese newspaper on December 21, 1995.
Features of Ruby :
Pro's of Ruby :
Con's of Ruby :
Setup & Installation :
- Object-oriented
- Flexibility
- Visual appearance
- Dynamic typing
- Exception handling
- Garbage collector
- Portable
- Keywords
- Variable constants
- Naming conventions
- Keyword arguments
- Method names
- Singleton methods
- Case Sensitive
Pro's of Ruby :
- Highly readable
- Ruby is fast to write
- Everything is an object
- Maintainability
- Simple debugging
- Great application performance
- Built-in data structures
- Comes with dependency management
Con's of Ruby :
- Runtime Speed
- Boot Speed
- Documentation
- Scalability
Setup & Installation :
- Go to official website of ruby on rails and click on download.
- Accept the licence.
- Choose the installation path and click on install.
- Select the components and click on next.
- Click on finish.
- Program to print hello ruby.
Basic concepts in Ruby :
Variable :
- Variables are locations which hold data to be used in the programs. Each variable has a different name. Unlike other programming languages, there is no need to declare a variable in Ruby. A prefix is needed to indicate it.
- There are four types of variables.
- Local variables
- Class variables
- Instance variables
- Global variables
Local | Global | Instance | Class | |
Scope | Limited within the block of initialization. | Its scope is globally. | It belongs to one instance of a class. | Limited to the whole class in which they are created. |
Naming | Starts with a lowercase letter or underscore (_). | Starts with a $ sign. | Starts with an @ sign. | Starts with an @@ sign. |
Initialization
| No need to initialize. An uninitialized local variable is interpreted as methods with no arguments. | No need to initialize. An uninitialized global variable will have a nil value. | No need to initialize. An uninitialized instance variable will have a nil value. | They need to be initialized before use. An uninitialized global variable results in an error. |
Data types :
- Data types represents a type of data such as text, string, numbers, etc.
- There are different data types.
- Numbers
- Strings
- Symbols
- Hashes
- Arrays
- Boolean
Numbers : Integers and floating point numbers come in the category of numbers. Integers are held internally in binary form. Integer numbers are numbers without a fraction.
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 (:).
- 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.
Control Statements :
Until Loop :
- The until loop runs until the given condition evaluates to true. It exits the loop when condition becomes true.
- It is just opposite of the while loop which runs until the given condition evaluates to false.
- The until loop allows you to write code which is more readable and logical.
Break Statement :
- The break statement is used to terminate a loop. It is mostly used in while loop where value is printed till the condition is true, then break statement terminates the loop.
- The break statement is called from inside the loop.
Next Statement :
- The next statement is used to skip loop's next iteration. Once the next statement is executed, no further iteration will be performed.
- The next statement in Ruby is equivalent to continue statement in other languages.
Redo Statement :
- Ruby redo statement is used to repeat the current iteration of the loop.
- The redo statement is executed without evaluating the loop's condition.
- It is used inside a loop.
Methods :
- Methods prevent us from writing the same code in a program again and again. It is a set of expression that returns a value.
- Ruby methods are similar to the functions in other languages. They unite one or more repeatable statements into one single bundle.
- To use a method, we need to first define it. Ruby method is defined with the def keyword followed by method name. At the end we need to use end keyword to denote that method has been defined.
- Methods name should always start with a lowercase letter. Otherwise, it may be misunderstood as a constant.
- Syntax : def methodName.....code... end.
- string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.
- string literals are enclosed within single and double quotes.
- Ex : puts 'Hello everyone' ( or ) puts "Hello everyone".
- In Ruby, by default strings are not immutable. To make them immutable, freeze method can be used.
Arrays :
- Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.
- It's indexing starts with 0. The negative index starts with -1 from the end of the array. For example, -1 indicates last element of the array and 0 indicates first element of the array.
- A Ruby array is created in many ways.
- Using literal constructor [] (EX : exm = [4, 4.0, "Jose")
- Using new class method (EX : arrayName = Array.new(10))
Hashes :
- A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type.
- If a hash is accessed with a key that does not exist, the method will return nil.
- EX : 1) name = {"key1" => "value1", "key2" => "value2", "key3" => "value3"...} 2) name = {key1: 'value1', key2: 'value2', key3: 'value3'...}.