Ques 11. How will you implement Observer Pattern in Ruby on Rails?
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
Ques 12. What is the purpose of Environment.RB and Application.RB in Ruby on Rails?
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.
Ques 13. How can you define a Constant in Ruby on Rails?
Create a new file as shown below under: config/initializers/my_constants.rb
COLORS = ['white', 'red', 'green']
Ques 14. How can you fire a method when a module is inside a class?
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.
Ques 15. What is the default access modifier for a method?
By default all methods are public, except the initialize(constructor) method.
You can make methods private using this declaration within your class:
class MyClass
def method_public_here
end
PRIVATE# all methods that follow will be made private: not accessible for outside objects
def method_private_here
end
end