Ruby is considered as the best combination of Smalltalk’s conceptual elegance Python’s ease of use, the learning, and Perl’s pragmatism.
Ruby will be a high-level programming language, which is explained like Perl, Python, Tcl/TK, and object oriented like Smalltalk, Eiffel, Ada, and Java.
Ruby was first created in Japan and now it is getting popular in US and Europe as well. The following factors contribute towards its popularity
- Easy to learn
- Open Source
- Rich libraries
- Very easy to extend
- Truly object-oriented
- Less coding with fewer bugs
- Helpful community
There are various reasons to use Ruby, as well as a few drawbacks as well that we may have to consider before implementing ruby
Performance Issues– These are enemies of Perl and Python, it is still an explained language and we cannot compare it with high-level programming languages like C or C++.
Threading model- Ruby may not use local threads. These threads are simulated in the VM as of running as native OS threads.
Consider an example
# The Hello Class
class Hello
def initialize( name )
@name = name.capitalize
end
def salute
puts “Hello #{@name}!”
end
end
# Create a new object
h = Hello.new(“Rubyeee”)
# Output “Hello Rubyee!”
h.salute
Output It produces the following result −
Hello Rubyeee!
Embedded Ruby
Here ruby provides a program called ERB(Embedded Ruby). It allows us to put ruby codes inside an HTML file.ERB reads along, word for word, and then at a certain point, when it uses ruby code attached in the document, it starts executing the Ruby code.
We should know two things here to prepare the ERB document
We want some Ruby code executed, enclose it between<% and %>
We want the result of the code execution to be printed out as a part of output, enclose the code between <%and%>
Consider an example
<% page_title = “Demonstration of ERB” %>
<% salutation = “Dear programmer,” %>
<html>
<head>
<title><%= page_title %></title>
</head>
<body>
<p><%= salutation %></p>
<p>This is a sample of how ERB fills out a template.</p>
</body>
</html>
Now execute the program by using the command-line utility erb.
tp> erb erbdemo.rb
It has a result
<html>
<head>
<title>Demonstration of ERb</title>
</head>
<body>
<p>Dear programmer,</p>
<p>It is example of how ERb fills out a template.</p>
</body>
</html>
What is Rails?
A highly productive web-application framework. It is written in Ruby by David Heinemeier Hansson. It builds a web application for a minimum of ten times faster with Rails than we could with a typical Java framework. An open-source Ruby framework for developing database-backed web applications. Configure your code with Database Schema. No compilation phase is required.
Full-stack Framework:
It includes everything needed to create a database-driven web application, using the Model-View-Controller pattern. Being a full-stack framework means all the layers are built to work seamlessly together with less code. Requires fewer lines of codes than frameworks.
Convention over configuration
Rails always turn the configuration files in favor of conventions, reflection, and dynamic runtime extensions. Our application code and our running database already contain everything that Rails needs to know.
Rails Strength
Rail is packed with features that make you more productive, with many following features building on others.
1.MetaProgramming
2. Active Record
3. Convention over Configuration
4. Scaffolding
5. Built-in testing