Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques 21. What is the purpose of YIELD in Ruby on Rails?


The interpreter essentially invokes a separate piece of code and places it in the location. You might say it is similar to a method calling another method. Let’s understand a little bit of background about where YIELD might be useful first. 

The Rails framework encourages you to write code that is DRY (Don’t Repeat Yourself).

Developers often write common code in a central file and then they write the custom code in the specific files. Let’s say you are building a web application and you want all pages to have a common header, a common footer, the same “Welcome user-name!” message.

You can put all this common code in your application.html.erb file.

<html> .... common page title
.. standard header... 
..common page title,
 <%= YIELD %>
..footer code can go here... </body> 

The rest of the custom code can go in your specific file. Say the page you are creating is the list of articles. Then in your implementation file you would just write the code for pulling in the articles and the final page displayed to the user would be your custom code which will be placed instead of the <%= YIELD %>code in the application.html.erb file.

Is it helpful? Add Comment View Comments
Ques 22. What are Class Variables? How to define them?

Class variables are created using the @@ prefix to denote the variable as class level. 

It works just like any other variable, however in the case of inheritance it works more like a static variable that is accessed across all variable instances.

Another example can be found here:

class DemoClass 
 @@my_var = nil
  def initialize
    @@my_var = "hello world"
  def my_var
    puts @@my_var
class Demo2Class < DemoClass
  def initialize
    @@my_var = "goodbye world"
demo1 =
demo2 =
The output would be as shown below:
hello world
goodbye world
goodbye world
Is it helpful? Add Comment View Comments
Ques 23. How to define Instance variables?

Instance variables are defined using single @ symbol.

@foo = "Hello"

Within a class they can be declared as below:

class Animal
 attr_accessor :name, :age

Next you can query an object instance to find which instance variables it has.

anim =
=> [ ]"John"
anim.age = 3
 => [:@age, :@name] 

In the above case we did not put the @ symbol before the instance variables but it is implied. 

Is it helpful? Add Comment View Comments
Ques 24. How can you dynamically define a method body in Ruby on Rails?

An instance method can be defined dynamically with

Module#define_method(name, body),

where name is the method’s name given as a Symbol, and body is its body given as a Proc, Method, UnboundMethod, or block literal. This allows methods to be defined at runtime, in contrast to def which requires the method name and body to appear literally in the source code.

class Conjure
  def self.conjure(name, lamb)
    define_method(name, lamb)

# Define a new instance method with a lambda as its body

Conjure.conjure(:glark, ->{ (3..5).to_a * 2 }) #=> [3, 4, 5, 3, 4, 5]  

Module#define_method is a private method so must be called from within the class the method is being defined on. Alternatively, it can be invoked inside class_eval like so:

Array.class_eval do
  define_method(:second, ->{ self.[](1) })
[3, 4, 5].second #=> 4

Kernel#define_singleton_method is called with the same arguments as Module#define_method to define a singleton method on the receiver.

File.define_singleton_method(:match) do |file, pattern|
File.match('/etc/passwd',/root/) #=> #<MatchData "root">
Is it helpful? Add Comment View Comments
Ques 25. What is Range?

Range is a great way to declare continuous variables. You should use it to declare arrays and other types of collections. 

range1 = (1..4).to_a
 => [1, 2, 3, 4] 
puts range1

You can also create strings in this format and it fills in the interim values automatically.

range2 = ('bar'..'bat').to_a
puts range2
Since the end result of using range is an array you can also iterate over it just like any other array.
range2.each do |str|
   puts "In Loop #{str}"

This produces the result as shown below:

In Loop bar
In Loop bas
In Loop bat
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2018 WithoutBook