Ruby is a beautiful language which can be written like poems and it is difficult to run an exploit. In other words, Ruby is object-oriented, reflective, general-purpose, dynamic programming language which is secure. Julia, Swift, Rust etc newer languages like Ruby. Due to the initial lack of English documentation, the language spread mainly in the Japanese-speaking world, where it has since become more popular than its Western counterpart Python. It wasn’t until the advent of the Ruby on Rails web framework and the need for fast web development that Ruby became widespread in the Western world.
During the development of Ruby, the focus was on the uniformity and readability of the source code. This applies in particular to conventions for naming variables and methods as well as a very uniform and consistent syntax. In Ruby, the advantages of object-oriented and functional programming are combined; this makes it possible to describe and solve many problems very easily.
There is a large number of available libraries that, in conjunction with RubyGems, allow you to avoid having to solve common problems yourself through easy availability. This means that a large number of applications can be developed in Ruby, from console scripts and graphical applications to network and Internet applications.
---
Installation of Ruby
Windows: Open Ubuntu bash, run apt-get install ruby
command.
Linux: Open the terminal, run apt-get install ruby
command or yum install ruby ruby-devel
MacOS: Under MacOS X, a Ruby interpreter is installed with the operating system. However, you can install the latest Ruby managed via Homebrew (it sounds sarcastic though, Homebrew is written in Ruby).
Open iTerm2 (with homebrew installed), run brew install ruby
Optionally, in ~/.bash_profile
(or .profile
or .zshrc
) add the following line:
1 | export PATH=/usr/local/Cellar/ruby/2.3.1p112/bin:$PATH |
2.3.1p112
is the version of Ruby, which can be found by”
1 | ruby --version |
You can source the file to reload the settings:
1 | source ~/.bash_profile |
Learning Ruby : Chapter 1
Type irb
on terminal or bash or iTerm2, whatever you use. irb
means interactive Ruby. Type 2+4
and hit enter. The basic calculations can be directly done on Ruby.
puts
in Ruby is the print
in other languages. But it is not like a dumb print
of other languages. Type:
1 | puts "Hello World" |
Hit Enter. You’ll get the output:
1 2 | Hello World => nil |
Now, run:
1 | puts "Hello World"; 24*365 |
You’ll get the output:
1 2 | Hello World => 8760 |
Now, run:
1 | "Hello World" |
You’ll get the output:
1 | => "Hello World" |
Now, run:
1 | "Hello World"; 24*365 |
What will be the output?
OK. Now do this:
1 | 24*365; "Hello World" |
What do you think?
#
is used to add comments to your code in Ruby or to insert a dynamic value with #{thing}
.=begin
, =end
is used to indicate multi-line comments.
Now, type these lines one by one and hit Enter:
1 2 3 | def hi puts "Hello World!" end |
This means hi is related to the string Hello World!
. If you type hi
or hi()
and hit Enter, it will return you Hello World!
. def
in this context is the definition of the method, hi
is the name of the method. Now if this hi
defined a complex calculation, we could easily grab the value by calling hi
.
Time.now
prints the present time. We can run:
1 | puts "I have #{24+300/6} cents in my PayPal account at" #{Time.now} |
Without ,
or ;
before Time.now
there will be syntax error. Now type exit
to exit the irb
and run:
1 | nano test.rb |
Paste this content:
1 2 3 4 5 6 7 | if 1 > 2 puts "1 is greater than 2" elsif 2 > 1 puts "1 is not greater than 2" else puts "1 is equal to 2" end |
Then execute the script:
1 2 | chmod +x test.rb ruby test.rb |
This ends the first chapter.