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
  end
  
  def read_config_file
#    your code to read a config and populate the variable
    @config = false
  end
  
  def convert_to_erb
    template_file = File.open("test.xml.erb", "r").read
    erb = ERB.new(template_file)
    File.open("test.xml","w+") { |file| 
      file.write(erb.result(binding))
    }
  end
  
end

creator = Customiser.new
creator.read_config_file()
creator.convert_to_erb()

 

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

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

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: 7012

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
    else
      wordCount[word] = (wordCount[word]+1)
    end
  end
  return wordCount
end

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: 4372

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! 
Malayalam
Glenelg 

  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
end

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: 5247

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: 22505

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: 4406
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest