Log in
updated 11:29 AM UTC, May 4, 2016

Ruby conditional statements in ERB file

Here is another program to demonstrate the use of conditional statements in Ruby ERB file.

This program converts the ERB file into an xml file.

require 'rubygems'
require 'erb'

class Customiser
  attr_accessor :config
  def initialize
    @config = true
  def read_config_file
#    your code to read a config and populate the variable
    @config = false
  def convert_to_erb
    template_file = File.open("test.xml.erb", "r").read
    erb = ERB.new(template_file)
    File.open("test.xml","w+") { |file| 

creator = Customiser.new


Here is the ERB file (test.xml.erb):

	<table name="something">
		<% 	if @config == true
				@val = "<choice-1> <choice-1>"		
			 	@val = "<choice-2> <choice-2>"
		 <%= @val %>

run the program by changing the config variable value to see the difference.

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Ruby
  • Hits: 8335

Ruby Program to count number of words in a String

Let us look into a program which counts the number of words in a String using Ruby.

def count_words(str)
  wordCount = {}
  str.downcase.scan(/\b[\w]*\b/).each do |word|
    next if  word.empty?
    if wordCount[word] == nil || wordCount[word] == ''
     wordCount[word] = 1
      wordCount[word] = (wordCount[word]+1)
  return wordCount

The above function takes a String as its argument and process it word by word (regex - '\b[\w]*\b').

For each word it adds it to a Hash with value 1( if not already present) or increment the value counter (if already present).

Test the function by adding code as following

count_words("A man, a plan, a canal -- Panama")

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Ruby
  • Hits: 5143

Ruby program to check if String is a palindrome

Definition of Palindrome:

 A word, phrase, verse, or sentence that reads the same backward or forward. For example:

A man, a plan, a canal, Panama! 

  Ruby provides easy way to check if a String is a Palindrome. Below lines of code is all that is needed to check if String is a palindrome. lets go through it:

def palindrome?(str)
  onlyLetters = str.downcase.scan(/\w/)
  onlyLetters == onlyLetters.reverse

This function just ignores the case (str.downcase) and takes only letters from the string (.scan(/\w/)).

str.scan(/\w/) uses regular expression to select only the letters.

once it is done , its easy to reverese and check if the value is equal to the string. Perhaps the shortest code to do it this efficiently.


Add a comment (0)

  • Written by Madhu V Rao
  • Category: Ruby
  • Hits: 6167

Simple example to explain Ruby's ERB templating system

ERB is an abbreviation for Embedded Ruby

ERB is a feature of Ruby that enables user to dynamically generate any kind of text from templates. The templates combine plain text with Ruby code for variable substitution and flow control, which makes them easy to write and maintain.

ERB is commonly used in Rails platform to generate the webpages, It is also used in generating XML files, Source code, etc.

Here I will be explaining a simple program which generate an HTML file from a template using a data file.

The below Ruby program produces an HTML file which shows the list of languages of India in a Table format. The input to the Ruby program is a template file (.erb) and a data.xml which has list of languages.

Add a comment (2)

  • Written by Madhu V Rao
  • Category: Ruby
  • Hits: 23935

How to download Images using Ruby and how Threads help in downloading them faster

Just tried the same program which was done in Java earlier. As you can see number of Lines in Ruby program is much less compared to Java , though its little complex initially to understand the Ruby program , as you get familiar with the internals of the Ruby , Its quite easy to understand. 

Below program is quite self explanatory which uses many language constructs of Ruby like Class, Threads, Arrays , etc

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Ruby
  • Hits: 5100
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest