- Class is an object too
- To some, it's also the root class in ruby (Object).
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
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 object are not garbage collected in ruby
. Params should not be mapped directly rather slice it first to avoid memory overflow.
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
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.
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.
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
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)
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
.