Copyright © tutorialspoint.com
Before we ride on Rails, let's understand a little about Ruby Programming Language which is the base of Rails.
Ruby is a interpreted programming language developed by Yukihiro Matsumoto in 1995 and today Ruby is the fastest growing language.
Ruby is the successful combination of:
Ruby is :
Ruby is becoming popular exponentially in Japan and now in US and Europe as well. According to the TIOBE Programming Community Index, it is the fastest growing language.
Following are greatest factors which are making this language the fastest growing one among the other languages:
Performance - Although it rivals Perl and Python but because of being interpreting language, we can not compare it with high programming lanaguage like C or C++.
Threading Model - Ruby does not use native threads. This means Ruby threads are simulated in the VM rather than running as native OS threads.
Here is a sample Ruby code to print "Hello Ruby"
#!/usr/bin/ruby -w # The Hello Class class Hello # Define constructor for the class def initialize( name ) @name = name.capitalize end # Define a ruby method def salute puts "Hello #{@name}!" end end # Create a new object for Hello class obj = Hello.new("Ruby") # Call ruby method obj.salute |
This will produce following result:
Hello Ruby |
For a complete understanding on Ruby, please go through our Ruby Tutorial
Ruby provides you with a program called ERb (Embedded Ruby), written by Seki Masatoshi. ERb allows you to put Ruby code inside an HTML file. ERb reads along, word for word, and then at a certain point when it sees the Ruby code embedded in the document it sees that it has to fill in a blank, which it does by executing the Ruby code.
You need to know only two things to prepare an ERb document:
If you want some Ruby code executed, enclose it between <% and %>
If you want the result of the code execution to be printed out, as part of the output, enclose the code between <%= and %>.
Here's an example, Save the code in erbdemo.erb file. Please note that a ruby file will have extension .rb but Embeded Ruby file can have extension as .erb.
<% page_title = "Demonstration of ERb" %> <% salutation = "Dear programmer," %> <html> <head> <title><%= page_title %></title> </head> <body> <p><%= salutation %></p> <p>This is an example of how ERb fills out a template.</p> </body> </html> |
Now, run the program using the command-line utility erb
c:\ruby\>erb erbdemo.erb |
This will produce the following result:
<html> <head> <title>Demonstration of ERb</title> </head> <body> <p>Dear programmer,</p> <p>This is an example of how ERb fills out a template.</p> </body> </html> |
Copyright © tutorialspoint.com