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 26. How can you implement method overloading in Ruby on Rails?

Ans.

This one’s a tricky question. If you have a background in Java then you must know that method overloading is simply multiple methods with same name but different signatures/parameters.

In the case of Ruby method overloading is not supported. 

However, it does support the overall goal of passing variable number of parameters to the same method. You would implement it like this:

class MyClass  
  def initialize(*args)  
    if args.size < 2  || args.size > 3  
      puts 'This method takes either 2 or 3 arguments'  
    else  
      if args.size == 2  
        puts 'Found two arguments'  
      else  
        puts 'Found three arguments'  
      end  
    end  
  end  
end  

The output can be seen here:

MyClass.new([10, 23], 4, 10)  
Found three arguments
MyClass.new([10, 23], [14, 13]) 
Found two arguments

SO: You can get the same effect as method overloading but you just have to manage the number of variables inside your method itself.

Is it helpful? Add Comment View Comments
Ques 27. What is the difference between ‘&&’, ‘AND’ and ‘&’ operators?
Ans.

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 28. What is the convention using ‘!’ at the end of method name?
Ans.

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 29. Does Ruby support Multiple Inheritence?
Ans. Ruby does not support multiple inheritance.
Is it helpful? Add Comment View Comments
Ques 30. How can you achieve the same as Multiple Inheritance using Ruby? What is Mixin?
Ans.
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

Most helpful rated by users:

©2016 WithoutBook