Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques 6. What is the difference between ‘&&’, ‘AND’ and ‘&’ operators?

The ‘&&’ and ‘and’ are both logical and statements. They ‘&&’ operator has higher precedence though. Here’s an example of illustrate this in more detail:

foo = 3
bar = nil
a = foo and bar
# => nil
a
# => 3
a = foo && bar
# => nil
a
# => nil
Notice how the statement ‘a = foo and bar’ actually behaves like ‘(a = foo) and bar’

Is it helpful? Add Comment View Comments
 

Ques 7. What is the convention using ‘!’ at the end of method name?

The ! indicates that the method is about to change the object itself.

Here’s an example:

foo = "A TEST STRING"  # a string called foo

foo.downcase!     # modifies foo permanently
a test string

puts foo          # prints modified foo
a test string

Similarly if you did not want the object to be changed you could have something simple like:

foo2 = "A 2nd Test String"  # a string called foo 

foo2.downcase     # modifies foo temporarily
a 2nd test string 

puts foo2 nbsp;    # prints original foo 
A 2nd Test String

Is it helpful? Add Comment View Comments
 

Ques 8. Does Ruby support Multiple Inheritence?

Ruby does not support multiple inheritance.

Is it helpful? Add Comment View Comments
 

Ques 9. How can you achieve the same as Multiple Inheritance using Ruby? What is Mixin?

Ruby offers a very neat alternative concept called mixin. Modules can be imported inside other class using mixin. They are then mixed-in with the class in which they are imported.

Here’s an example:

module Debug
  def whoAmI?
    "I am #{self.to_s}"
  end
end

class Photo
 include Debug
end

ph = Photo.new

"I am : #<Photo:0x007f8ea218b270>"

As you can see above the class Debug and it’s method “whoamI?” were mixed-in (added) with the class Photo.

That’s why you can now create an instance of the Photo class and call the whoAmI? method.

ph.whoAmI?
 => "I am : #<Phonograph:0x007f8ea218b270>" 

Is it helpful? Add Comment View Comments
 

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

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
 

Most helpful rated by users:

©2021 WithoutBook