Skip to content

Instantly share code, notes, and snippets.

@Reznov9185
Last active January 21, 2017 15:48
Show Gist options
  • Save Reznov9185/fefd61430af5ea78b658198d18a7ba68 to your computer and use it in GitHub Desktop.
Save Reznov9185/fefd61430af5ea78b658198d18a7ba68 to your computer and use it in GitHub Desktop.
A quick trip to the basic interview questions on RoR.

What is an object and class to Ruby?

  1. Class is an object too
  2. To some, it's also the root class in ruby (Object).

What is a module? Can you tell me the difference between classes and modules?

Modules serve as a mechanism for namespaces.

module ANamespace
  class AClass
    def initialize
      puts "Another object, coming right up!"
    end
  end
end

ANamespace::AClass.new
 #=> Another object, coming right up!

Also, modules provide as a mechanism for multiple inheritance via mix-ins and cannot be instantiated like classes can.

module AMixIn
  def who_am_i?
    puts "An existentialist, that's who."
  end
end

# String is already the parent class
class DeepString < String
  # extend adds instance methods from AMixIn as class methods
  extend AMixIn
end

DeepString.who_am_i?
 #=> An existentialist, that's who.

AMixIn.new
 #=> NoMethodError: undefined method ‘new’ for AMixIn:Module

What is the difference between Ruby’s Hash and ActiveSupport’s HashWithIndifferentAccess?

The Hash class in Ruby’s core library retrieves values by doing a standard == comparison on the keys. This means that a value stored for a Symbol key (e.g. :my_value) cannot be retrieved using the equivalent String (e.g. 'my_value'). On the other hand, HashWithIndifferentAccess treats Symbol keys and String keys as equivalent so that the following would work:

h = HashWithIndifferentAccess.new
h[:my_value] = 'foo'
h['my_value'] #=> will return "foo"

Symbol vs String objects in Ruby?

Symbol object are not garbage collected in ruby. Params should not be mapped directly rather slice it first to avoid memory overflow.

Give an example of n+1 bug in Ruby and how to avoid it

Example:

class CommentsController < ApplicationController
  def users_comments
    posts = Post.all
    comments = posts.map(&:comments).flatten
    @user_comments = comments.select do |comment|
      comment.author.username == params[:username]
    end
  end
end

Soln.

posts = Post.includes(comments: [:author]).all

What is CSRF? How does Rails protect against it?

CSRF stands for Cross-Site Request Forgery. This is a form of an attack where the attacker submits a form on your behalf to a different website, potentially causing damage or revealing sensitive information. Since browsers will automatically include cookies for a domain on a request, if you were recently logged in to the target site, the attacker’s request will appear to come from you as a logged-in user (as your session cookie will be sent with the POST request).

In order to protect against CSRF attacks, you can add protect_from_forgery to your ApplicationController. This will then cause Rails to require a CSRF token to be present before accepting any POST, PUT, or DELETE requests. The CSRF token is included as a hidden field in every form created using Rails form builders. It is also included as a header in GET requests so that other, non-form-based mechanisms for sending a POST can use it as well. Attackers are prevented from stealing the CSRF token by browsers’ “same origin” policy.

Separating the professional from the hobbyist

Senior programmers should be able to give competent answers for all questions. Junior programmers should answer some correct, but usually won't know them all.

Explain this ruby idiom: a ||= b

A common idiom that strong ruby developers use all the time.

# a = b when a == false
# otherwise a remains unchanged
a || a = b
a = 1
b = 2
a ||= b #=> a = 1
a = nil
b = 2
a ||= b #=> a = 2
a = false
b = 2
a ||= b #=> a = 2

What is a Proc?

Everyone usually confuses procs with blocks, but the strongest rubyist can grok the true meaning of the question.

Essentially, Procs are anonymous methods (or nameless functions) containing code. They can be placed inside a variable and passed around like any other object or scalar value. They are created by Proc.new, lambda, and blocks (invoked by the yield keyword).

Note: Procs and lambdas do have subtle, but important, differences in ruby v1.8.6. However, I wouldn't expect a candidate talk about these nitty-gritty details during an interview. (Kudos to Noah Thorp)

# wants a proc, a lambda, AND a block
def three_ways(proc, lambda, &block)
  proc.call
  lambda.call
  yield # like block.call
  puts "#{proc.inspect} #{lambda.inspect} #{block.inspect}"
end

anonymous = Proc.new { puts "I'm a Proc for sure." }
nameless  = lambda { puts "But what about me?" }

three_ways(anonymous, nameless) do
  puts "I'm a block, but could it be???"
end
 #=> I'm a Proc for sure.
 #=> But what about me?
 #=> I'm a block, but could it be???
 #=> #<Proc:0x00089d64> #<Proc:0x00089c74> #<Proc:0x00089b34>

Student.find(1) vs Student.find_by_id(1) if we delete the Student with id=”1”

Student.find(1) will raise an error ActiveRecord::RecordNotFound: Couldn't find User with id=1 whereas Student.find_by_id(1) will not raise an error and will return nil.

References

  1. https://gist.github.com/ryansobol/5252653#file-gistfile1-md
  2. https://www.toptal.com/ruby-on-rails/interview-questions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment