Back to Home

Ruby Interview Cheatsheet I

Class vs Object

Class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

Variables in a Ruby Class

Instance Variables: Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name.

Class Variables: Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name. We don't really use class variable much..

class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end end

Ruby Pseudo-Variables

They are special variables that have the appearance of local variables but behave like constants. You can not assign any value to these variables.

  • self: The receiver object of the current method.
  • true: Value representing true.
  • false: Value representing false.
  • nil: Value representing undefined.
  • __FILE__: The name of the current source file.
  • __LINE__: The current line number in the source file.
  • Module

    Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits.

  • Modules provide a namespace and prevent name clashes.
  • Modules implement the mixin facility.
  • Modules define a namespace, a sandbox in which your methods and constants can play without having to worry about being stepped on by other methods and constants.

    ☕Module written in support.rb file module Week FIRST_DAY = "Sunday" def Week.weeks_in_month puts "You have four weeks in a month" end def Week.weeks_in_year puts "You have 52 weeks in a year" end end ☕!/usr/bin/ruby $LOAD_PATH << '.' require "support" class Decade include Week no_of_yrs=10 def no_of_months puts Week::FIRST_DAY number=10*12 puts number end end d1=Decade.new puts Week::FIRST_DAY =>"Sunday" Week.weeks_in_month =>"You have four weeks in a month" Week.weeks_in_year =>"You have 52 weeks in a year" d1.no_of_months =>120

    Mixins in Ruby

    Ruby does not support multiple inheritance directly but Ruby Modules have another wonderful use called a mixin.

    module A def a1 end end module B def b1 end end class Sample include A include B def s1 end end samp=Sample.new samp.a1 samp.b1 samp.s1

    Self

    Class methods do not have access to instance methods or instance variables. However instance methods can access both class methods and class variables.

    class Foo def self.bar // or class << self puts 'class method' end def baz puts 'instance method' end end

    Self can also help to organize classes by separating responsibilities. Here is the example.

    # app/models/order.rb class Order def converter OrderConversion.new(self) end end # app/models/order_conversion.rb class OrderConversion attr_reader :order def initialize(order) @order = order end def to_pdf ... end end

    In order to convert order to pdf @order.converter.to_pdf In object-oriented circles, this is known as composition.