Hire Ruby-on-Rails Developers

Have an upcoming Ruby on Rails interview for a Ruby On Rails web developer position?

Experienced developers at Auction Software have gathered together to collaborate on this list of Ruby on Rails interview questions to help you prepare for your Ruby on Rails technical interview. Can you answer them all?

1. What is metaprogramming?

Ruby developers should know what’s metaprogramming, because it is widely used, especially in popular frameworks such as Rails, Sinatra, and Padrino. By using metaprogramming, we can reduce duplicate code, but there is a downside where it will increase the complexity of the code in the long run.

Here’s an example of metaprogramming in Ruby:

A user can have a lot of roles, and you want to check the authorization.

Normal scenario:

def admin?

role == 'admin'

end

def marketer?

role == 'marketer'

end

def sales?

role == 'sales'

end

Metaprogramming:

['admin', 'marketer', 'sales'].each do |user_role|

define_method "#{user_role}?" do

role == user_role

end

end

2. class implementation

class Human

def talk

puts "I’m talking"

end

private

def whisper

puts "I’m whispering"

end

end

What’s the output of :

  1. Human.new.talk -> I’m talking
  2. Human.new.whisper -> NoMethodError: private method ‘whisper’ called for #<Human:0x007fd97b292d48>
  3. Human.new.send(:talk) -> I’m talking
  4. Human.new.send(:whisper) -> I’m whispering

To explain, the class object Human.new.talk is calling an instance method, so it works perfectly. The talk method is a public method and can be called by everyone.

The class object Human.new.whisper is trying to access a private method, which it is not allowed to. Private and Protected methods are not accessible from outside. They are only used internally. This is an object-oriented design and can be used to structure the code, which the implementation is not supposed to expose to consumer object.

Finally, Human.new.send(:talk) sends a bypass control check to the method so it can be called without raising an error. Same goes to Human.new.send(:whisper).

3. Explain how Symbol is different from variables?

Symbol is different from variables in following aspects

  • It is more like a string than variable
  • In Ruby string is mutable but a Symbol is immutable
  • Only one copy of the symbol requires to be created
  • Symbols are often used as the corresponding to enums in Ruby

4. Explain what is Cross-Site Request Forgery (CSRF) and how Rails is protected against it?

CSRF is a form of attack where hacker submits a page request on your behalf to a different website, causing damage or revealing your sensitive data. To protect from CSRF attacks, you have to add “protect_from_forgery” to yourApplicationController. This will cause Rails to require a CSRF token to process the request. CSRF token is given as a hidden field in every form created using Rails form builders.

5. Mention what is the difference between the Observers and Callbacks in Ruby on Rails?

  • Rails Observers: Observers is same as Callback, but it is used when method is not directly associated to object lifecycle. Also, the observer lives longer, and it can be detached or attached at any time. For example, displaying values from a model in the UI and updating model from user input.
  • Rails Callback: Callbacks are methods, which can be called at certain moments of an object’s life cycle for example it can be called when an object is validated, created, updated, deleted, A call back is short lived. For example, running a thread and giving a call-back that is called when thread terminates.

6. Given the code below, how one might access the @foo variable from outside? Is it an instance variable, or class variable? What object this variable is defined on?

class Foo

class << self

@foo = 42

end

end

You can access the variable with
(class << Foo ; @foo ; end)

It’s an instance variable and defined on Foo’s Singleton method, or more specifically, the eigenclass of Foo.

Each class in Ruby has its own “eigenclass.” This eigenclass is derived from Classclass. Foo class is an instance of it’s eigenclass. This eigenclass has the only instance Foo; it as well has instance methods, defined as class methods on Foo, and it might have instance variables, e. g. @foo variable in this particular example.

7. Mention what is the difference between calling super() and super call?

  • super(): A call to super() invokes the parent method without any arguments, as presumably expected. As always, being explicit in your code is a good thing.
  • super call: A call to super invokes the parent method with the same arguments that were passed to the child method. An error will therefore occur if the arguments passed to the child method don’t match what the parent is expecting.

8. Explain the role of garbage collection in Ruby on Rails?

Garbage collection frees up memory for other processes by removing pointer programs and inaccessible objects left behind after a program has executed. This frees the programmer from having to track objects created dynamically during runtime.

9. Explain what is a class library in Ruby?

Ruby class libraries consist of a variety of domains, such as thread programming, data types, various domains, etc. These classes give flexible capabilities at a high level of abstraction, giving you the ability to create powerful Ruby scripts useful in a variety of problem domains. The following domains which have relevant class libraries are,

  • GUI programming
  • Network programming
  • CGI Programming
  • Text processing

10. Mention what is the difference between Procs and Blocks?

The difference between Procs and Blocks,

  • Block is just the part of the syntax of a method while proc has the characteristics of a block
  • Procs are objects, blocks are not
  • At most one block can appear in an argument list
  • Only block is not able to be stored into a variable while Proc can