Skip to content

Instantly share code, notes, and snippets.

@mike1011
Last active March 3, 2022 12:29
Show Gist options
  • Save mike1011/afa2f440868ecb444380b44c86afafc3 to your computer and use it in GitHub Desktop.
Save mike1011/afa2f440868ecb444380b44c86afafc3 to your computer and use it in GitHub Desktop.
Ruby on Rails - Interview questions
========== ONLY RUBY ==========
array vs hash - ordered indexed collection
convert hash to array = h.to_a
get even/odd entries from array -> select.each_with_index { |_, i| i.even? }
get hash keys in reverse order -> Hash[h.to_a.reverse] OR hash_array.keys.reverse
---replace strings --
value = "cat"
# Replace string at with ote.
value.sub!("at", "ote") // just first instance - cote
value = value.gsub("cat", "---") //all instances globally ---
--method arguments-- https://www.rubyguides.com/2018/06/rubys-method-arguments/
keyword arguments, no order -> write(file:, data:, mode: "ascii")
optional arguments, order maintained -> write(file, data, mode: "ascii")
variable arguments -> write(*args), where args is an array of values
variable arguments as keyword will be hash and NOT array -> write(**args), where args is a hash
modules - include/extend/prepend and using super
//access methods - class/instance - http://rubyblog.pro/2017/04/class-methods-and-instance-methods-by-including-one-module
public_send vs send
======= RENDERING =========
yield - identifies a section where content from the view should be inserted
for named yield, we need content_for, eg -
yield :head
content_for :head do
<title>A simple page</title>
end
render collections - eg
render partial: "product", collection: @products
----same as --- render @products
Layout rendering
Nested Layouts - eg -
content_for?(:content) ? yield(:content) : yield
-- add view file to render
<% content_for :content do %>
<%end%>
======== METAPROGRAMMING ========
method_missing
self
Ruby object hierarchy
class_eval vs instance_eval
monkey patch - how can you monkey patch == method so that it always returns false
def ==(val)
obj.val != val
end
======== ACTIVERECORD - query, validations, migration, optimization ===========
ActiveRecord - includes -> preload, eager_load
ActiveRecord - methods to skip validations
Migration - add, update, remove table columns
has_many vs has_many_through
STI vs Polymorphic associations
==== Exceptions in Ruby ============
ruby use sort-of try/catch block with begin/rescue to rescue exceptions, but do not rescue Exception directly.
>> read more here - https://www.honeybadger.io/blog/ruby-exception-vs-standarderror-whats-the-difference/
begin
@user = User.find_by!(id: 1)
rescue Exception => e # DONT DO THIS MISTAKE!
print e
end
======== Basic Rails, Ruby ============
procs vs lambda vs blocks
String vs Symbol
csrf_token
pluck vs select
find vs find_by
Design Patterns - Creational, Structural and Behavioral
require vs load
Examples of Restful api - for eg - give REST API's for the resource Account
Rails application using Multi databases - octopus, multidb etc.
Rails concerns - eg - in model, controller and routes.
Rails modules - includes, extends
Global Interpretor Lock in Ruby(GIL) - GIL or global interpreter lock makes it so only one thread runs at a time, preventing threads from running concurrently. Hence, the “lock” portion of the name. This can prevent race conditions from occurring, making it harder for data to become corrupt.
HTTP Headers - Content-Type(request - MIME type)/Accept(response)
what is extend/include/prepend module and how it works - https://medium.com/@leo_hetsch/ruby-modules-include-vs-prepend-vs-extend-f09837a5b073
Rails ActiveRecord::Validations - validates, validates_with(Class), validate(methods)
Ruby Singleton class - When you add a method to a specific object, Ruby inserts a new anonymous class into the inheritance hierarchy as a container to hold these types of methods.
ref - https://devalot.com/articles/2008/09/ruby-singleton
RUBY GC circular references
Ruby Malloc/Calloc
CSRF
CORS
ACID Properties
========== CACHING =============
Fragment(default)
Page
Action
Russian Doll Caching(Nested caching)
Low-level caching using Rails.cache
SQL Caching - rails caches every result of query in first attempt and return cached version if same query is executed, this is Action scoped.
Default - memory_store - cannot share across different process, hence not suitable for distributed environments
other cache store - file_store- saves on disk and require disk clean up to free up space.
other cache stores - mem_cache(dalli), redis-store(redis).
========= PERFORMANCE OPTIMIZATIONS ==========
Performance Optimization techniques
Memoization in Ruby - single/multi line
using .select instead of select *
======== TESTING ========
Rspec assertions example
Rspec describe, context, it blocks
Rspec before/after life cycle hooks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment