Without Book Interview Questions and Answers | Online Test | Moct Test
Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Institute Training Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques 31. How will you implement Single Pattern in Ruby on Rails?

Ans.

Singleton means single instance. 

So, the goal of a singleton pattern is to write a class definition but only allow the creation of the single instance of that object. 

This can be achieved nicely with the singleton gem as shown below:

require 'singleton'
 class Logger
  include Singleton
  def initialize
    @log = File.open("logfile.txt", "a")
  end
  def log(msg)
    @log.puts(msg)
  end
end

Adding the singleton as a mixin to the 

Logger.instance.log('This is just a test message')

The code above will create a single instance of Logger and simply put the message in the logger file.

Singleton patterns are mostly used for DB instance, Logger instance, etc. —- cases where there should be ONE and only ONE instance of the object that is used. 

Sometimes you might like to actually hold on to the logger object and use it everywhere you can do so by the following command:

logObj = Logger.instance

Notice you cannot use the Logger.new to create an object instance because this is a singleton object and therefore calling ‘new’ would fail.

Is it helpful? Add Comment View Comments
Ques 32. How will you implement Observer Pattern in Ruby on Rails?
Ans.

Let’s review first what an observer pattern is all about.  

The observer pattern (sometimes known as publish/subscribe) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

You might have used them in other programming languages as listener objects. You use them whenever a button is clicked on the screen and a method gets called automatically. 

As in the case of the singleton pattern, the observer pattern is also implemented by mixing in a module. 

In the Ruby implementation, the notifying class mixes in the Observable module, which provides the methods for managing the associated observer objects.

And, the observers must implement the update method to receive notifications.

Here’s an example. Say you want to send an SMS alert to users if a company stock drops then you can do something like this:

require "observer" 
require "observer" 
  class Ticker # Periodically fetch a stock price 
    include Observable 
 	attr_accessor :price 
    def initialize symbol, price 
      @symbol = symbol 
  	@price = price 
	end
    
	def run 
      lastPrice = nil 
      loop do 
        @price = @price+Random.rand(11) 
        print "Current price: #{price}\n" 
        if @price != lastPrice 
          changed                 # notify observers 
          lastPrice = @price 
           notify_observers(Time.now, @price) 
         end
       end 
    end 
  end
 
  class Warner
     def initialize ticker  
     ticker.add_observer(self)   # all warners are observers     
  end   
end 
  
class SMSAlert < Warner     
   def update time, price       # callback for observer         
      print "--- #{time.to_s}: SMS Alert for price: #{price}\n"     
   end   
end  

class EmailAlert < Warner     
   def update time, price       # callback for observer         
      print "+++ #{time.to_s}: Email Alert Price changed to #{price}\n"    
   end
 end
Is it helpful? Add Comment View Comments
Ques 33. What is the purpose of Environment.RB and Application.RB in Ruby on Rails?
Ans.

There are two files where variables and configuration settings are stored. 

- config/environment.rb : Environment settings go here

- config/application.rb : Application level global settings go here

config.time_zone = 'Central Time (US & Canada)'
config.i18n.default_locale = :de
config.filter_parameters += [:password] # ensures that passwords are not logged

The same file is also used for configuring various environment settings such as:

config.action_mailer.smtp_settings # various email settings go here 

What is the purpose of config/environments/development.rb file?

You would specify various config settings the development environment in this file.

 config.action_controller.perform_caching = false # to enable caching

This is because you typically do not want to enable caching in the development environment. 

The same config setting in the production environment would be equal to true. 

Is it helpful? Add Comment View Comments
Ques 34. How can you define a Constant in Ruby on Rails?
Ans.

Create a new file as shown below under: config/initializers/my_constants.rb

COLORS = ['white', 'red', 'green']
Is it helpful? Add Comment View Comments
Ques 35. How can you fire a method when a module is inside a class?
Ans.

Fire a method inside a class is very simple.

Say you have a module file trig.rb:

module Trig
  PI = 3.141592654
  def Trig.sin(x)
   # ..
  end
  def Trig.cos(x)
   # ..
  end
end

Now you simply import this module inside your class and invoke the method using the “module.method_name” syntax as shown below

require "trig"

class myclass
y = Trig.sin(Trig::PI/4)

This type of invocation ensures that the right module method gets called.

Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2016 WithoutBook