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 36. What is the default access modifier for a method?

Ans.

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

Is it helpful? Add Comment View Comments
Ques 37. How can you call the base class method from inside of its overridden method?
Ans.

If you are inside the overridden method in the derived class then a simple call to super will call the right method in the base class

class Parent
   def try_this()
      puts "parent"
   end
end

class Child < Parent
   def try_this()
      super()
      puts "child"
   end
end

ch = Child.new
ch.try_this()

This generates the output

parent
child

Now if you just want to call the base class without calling the derived class then the best way to do that is to simply assign an alias to the parent method like this:

class Parent
  def knox
    puts 'parent'
  end
end
 
class Child < Parent
   alias_method :parent_knox, :knox
   def knox
     puts 'child'
   end
end
 
ch = Child.new
ch.parent_knox
ch.knox

This allows you to call the base class method with the alias parent_knox and the derived class method knox can be called directly.

parent
child
Is it helpful? Add Comment View Comments
Ques 38. Define the Rails MVC implementation with an example.
Ans.

The MVC framework is an age-old architecture pattern that works very well for most applications. Rails has adopted the MVC pattern in its inherent design.

Stated Simply:

a) Model is where the data is the database

b) Controller is where the logic is for the application

c) View is where the data is used to display to the user

Is it helpful? Add Comment View Comments
Ques 39. What is Scope in Ruby on Rails?
Ans.

Scopes are nothing more than SQL scope fragments. By using these fragments one can cut down on having to write long queries each time you access content.

Say you typically access content as shown below:

@posts = Post.where("published_at IS NOT NULL AND posts.published_at <= "+ Time.now)

Ruby offers you a nice way to put the where condition inside a scope statement as shown below.

class Post < ActiveRecord::Base
  scope :published, lambda { 
    { :conditions =>
      ["posts.published_at IS NOT NULL AND posts.published_at <= ?", Time.now]
    }
  }  
  scope :recent, :order => "posts.published_at DESC"
end

Now you can simply access the published posts as: Post.published

@posts = Post.published

Also, you can access recent posts as 

@recent_posts = Post.recent
Is it helpful? Add Comment View Comments
Ques 40. CAN YOU GIVE AN EXAMPLE OF A CLASS THAT SHOULD BE INSIDE THE LIB FOLDER?
Ans. Modules are often placed in the lib folder. 
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2016 WithoutBook