Ruby Flashcards
WHAT ARE RUBY GEMS?
A gem is a piece of ruby code packaged as a library so that it can be imported and used by others in their programs.
One of the most common gems that I use is pry-rails for debugging.
What is an object? (or what is an instance?)
An instance of a class. (or a single and unique unit of a class).
Classes themselves descend from the Object root class.
What is a class?
Classes are blueprints in which individual instances of a class can be derived from.
Classes hold data, have methods that interact with that data, and are used to instantiate objects.
What is a module? Can you tell me the difference between classes and modules?
Like a class, a module contains data and may have methods that interact with that data, and can be thought as a library of methods (ex: ruby’s Math module). But modules cannot be instantiated like classes can, nor do they have instance variables like classes do, and cannot be subclassed. Modules can be used across many classes.
Classes are classified by what they are, modules are classified by what they do.
Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?
All methods, no matter the access control, can be accessed within the class. But what about outside callers?
Public methods enforce no access control – they can be called in any scope.
Protected methods are only accessible to other objects of the same class.
Private methods are only accessible within the context of the current object.
https://gist.github.com/ryansobol/5252653
There are three ways to invoke a method in ruby. Can you give me at least two?
The dot operator (or period operator), the Object#send method, or method(:foo).call
https://gist.github.com/ryansobol/5252653
Explain this ruby idiom: a ||= b
a = b when a == false
otherwise a remains unchanged
What does self mean?
- at the class level, self is the class.
- at the instance level, self is the instance in context (refers to the current object).
What is a Proc?
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).
https://gist.github.com/ryansobol/5252653
What is unit testing (in classical terms)? What is the primary technique when writing a test?
Unit testing, simply put, is testing methods – the smallest unit in object-oriented programming. Strong candidates will argue that it allows a developer to flesh out their API before it’s consumed by other systems in the application.
The primary way to achieve this is to assert that the actual result of the method matches an expected result.
What is your favorite ruby gem?
pry-rails. I’m always using this for debugging, and I like how it includes Rails specific methods (such as show model and show routes).
What are the 4 types of variables in ruby?
Global, local, class, instance
What’s the difference between a symbol and string?
Strings are mutable and symbols are not.
A symbol will exist for the duration of a session, while a string does not. For ex, if you call object_id on a symbol twice, it will output the same id twice. If you did this on a string, it will be 2 different IDs.
http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers
WHAT ARE CLASS VARIABLES? HOW DO YOU DEFINE THEM?
Class variables are created using the @@ prefix to denote the variable as class level. In the case of inheritance it works more like a static variable that is accessed across all variable instances.
http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers
HOW DO YOU DEFINE GLOBAL VARIABLES?
Global variables are defined using single $ symbol. It can be declared anywhere and used anywhere.