Help with your answer

What other question would you ask a new hire when you interview for Ruby on Rails position? Let's share together. Part III

=> asked by Jae Lee

Categories: ruby on rails   interview question  


<< back to questions

Ruby - Marshaling

Java features the ability to serialize objects, letting you store them somewhere and reconstitute them when needed. Ruby calls this kind of serialization marshalling. Saving an object and some or all of its components is done using the method Marshal.dump. Later on you can reconstitute the object using Marshal.load. Ruby uses marshaling to store session data.

Ruby - Garbage Collection

d = nil // makes d a nil reference, meaning it does not refer to anything

d =

d1 = d

d1 = nil // the Dog object is abandoned and eligible for Garbage Collection.

The Ruby object heap allocates a minimum of 8 megabytes. Ruby’s GC is called mark-and-sweep. The “mark” stage checks objects to see if they are still in use. If an object is in a variable that can still be used in the current scope, the object is marked for keeping. If the variable is long gone, off in another method, the object isn’t marked. The “sweep” stage then frees objects which haven’t been marked.

Ruby uses a conservative mark-and-sweep GC mechanism. There is no guarantee that an object will undergo garbage collection before the program terminates.

Ruby - Class Methods

The idea of a class method is that you send a message to the object that is the class rather than to one of the class’s instances.

Dog#bark- the instance method bark in the class Dog

Dog.color- the class method color, in the class Dog

Dog::color- another way to refer to the class method Dog

Ruby - Access Control

Public methods can be called by everyone - no access control is enforced.

Protected methods can be invoked only by objects of the defining class and its subclasses.

Private methods cannot be called with an explicit receiver - the receiver is always self.

Ruby - Overriding Methods

class A
     def a
          puts ‘In class A’
class B < A
     def a
          puts ‘In class B’

Ruby - Usage of super

When you invoke super with no arguments Ruby sends a message to the parent of the current object, asking it to invoke a method of the same name as the method invoking super. It automatically forwards the arguments that were passed to the method from which it’s called.

class Bicycle
     attr_reader :gears, :wheels, :seats
     def initialize(gears = 1)
          @wheels = 2
          @seats = 1
          @gears = gears
class Tandem < Bicycle
     def initialize(gears)
          @seats = 2

about 4 years ago, by Jae Lee

Ruby - variables

An instance variable name starts with an @ is just like a private property in C# class as it is visible to all methods inside the object

A class variable name starts with an @@

A global variable name starts with $

string array can be built using w%

Ruby - ranges

To see if some value falls within the interval represented by the range, we use ===

(1..10) === 5# true

(1..10) === 15# false

Ruby - Bang (!) methods

sort, sort!, upcase, upcase!

If you call the non-bang version of the method on the object, you get a new object.

If you call the bang version, you operate in-place on the same object to which you sent the message. This saves memory too.

Ruby - symbol

A symbol is the most basic Ruby object you can create. It’s just a name and an internal ID. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program. Symbols are more efficient than strings. Two strings with the same contents are two different objects, but for any given name there is only one Symbol object. This can save both time and memory.

If the identity of the object is more important, use a symbol.

If the contents of the object are important, use a string

Ruby - hashes

Compare with arrays, hashes have one significant advantage: They can use any object as an index. Whenever you would use a quoted string, use a symbol instead.

people =

people[:nickname] = ‘IndianGuru’

people[:language] = ‘Marthi’

people[:lastname] = ‘Talim’

h = { :nickname => ‘IndianGuru’, :language => ‘Marathi’, :lastname => ‘Talim’ }

h = { nickname: ‘IndianGuru’, language: ‘Marathi’, lastname: ‘Talim’ }

about 4 years ago, by Jae Lee

Sponsored Links