What is Ruby?
- Ruby is an open source language that can be used for web development.
- It is a high level language which means its readable just like english language.
- It is interpreted and so it does not need a compiler to run.
- Object oriented language which helps users to manipulate data structures called object in order to build and execute data.
- Scripting language designed by Matz.
How to declare a variable?
Just declare a variable by assigning a value to it and your done.
eg: my_num = 25
You can reassign it. For boolean variables just assign true or false. For string variables assign value within quotes.
eg: my_boolean = true
my_string = "Ruby"
What are the arithmetic operators in Ruby?
Mainly there are 6 operators in use:+, -, *, /, **(exponentiation), %(modulo)
Exponentiation means raising one number to the power of the other. eg: 2**2 = 4
Modulo means the remainder of division eg: 25%7 = 3
What are the methods are used to display text?
print "ruby"
puts "ruby" - adds a new line after output.
Comments begin with # . => in the comment indicates the vale retuned by the commented code.
eg: 1.class # => Fixnum: the number 1 is a Fixnum
Now lets start practicing ruby on our terminal:
To have an interactive ruby in terminal type the command after $ sign: $irb
Here you can type in ruby for eg:
$puts 2+3
Output: 4
=>nil
$quit() (to quit interactive ruby)
You can open a text editor from terminal:
nano helloworld.rb
This opens an editor and you can type program like below
print(1+2)
print(“\n”)
puts “Hello world”
control+o = Save file
control+x = Exit file
ruby helloworld.rb (command to run a program)
Hurrah! Its that cool.
Source: http://www.codecademy.com/
http://www.tutorialspoint.com/
O'reilly Ruby programming language