Skip to content

Instantly share code, notes, and snippets.

@karimmtarek
Last active August 29, 2015 14:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karimmtarek/2a60a5d6bfe005c991a1 to your computer and use it in GitHub Desktop.
Save karimmtarek/2a60a5d6bfe005c991a1 to your computer and use it in GitHub Desktop.
Interview_Questions_Ruby

#Ruby Interview Questions

What are blocks for?

Plain old english: Ways of grouping code we want to run. The simplest explanation for a block is that it is a chunk of code between {} or do...end, that can't be stored in a variable and isn't an object. A block is just part of the syntax of a method call. It doesn’t mean anything on a standalone basis and can only appear in argument lists.

What are the difference between p and puts?

The difference is subtle, but p prints the true value of an object, rather than a string-ified version of the object. This will make more sense soon, but for the sake of simplicity, we'll use the p command throughout the exercises, as we'll want to see the true values of our code returned to the output window.

What’s the difference between a lambda and a proc(short for procedure)?

They are both Proc objects. However, lambdas are a different ‘flavor’ of procs.*

  1. Lambdas check the number of arguments, while procs do not
  2. Lambdas and procs treat the ‘return’ keyword differently
    • return inside of a lambda triggers the code right outside of the lambda code
    • return inside of a proc triggers the code outside of the method where the proc is being executed

*reference

What is a singleton class?
Explain how ruby looks for a method when you call it on a receiver.
What’s the difference between inspect and to_s?
What’s the difference between include and extend? / What’s the difference between extending a module and including it?
  1. reference
  2. reference
what does self mean when used in a class

self is the "current object" and the default receiver of messages (method calls) for which no explicit receiver is specified. Which object plays the role of self depends on the context.*

*reference

Explain what a ||= b means

A common misconception is that a ||= b is equivalent to a = a || b, but it behaves like a || a = b.

In a = a || b, a is set to something by the statement on every run, whereas with a || a = b, a is only set if a is logically false (i.e. if it's nil or false) because || is 'short circuiting'. That is, if the left hand side of the || comparison is true, there's no need to check the right hand side.

*reference

Understand access modifiers and how they are used within the ruby language (Private, public, protected)

private: The method cannot be called outside class scope. The object send message to itself protected: You can call an object's protected methods as long as the default object self is an instance of the same class as the object whose method you're calling public: can be called from anywhere

reference

Why do some methods end with a bang (!) and others with question marks (?), what are they called and what do they do?

Bang methods are most commonly used to distinguish between a dangerous and a safe version of the same method.

  • mutator methods - one version changes the object, the other one returns a copy and leaves the original object unchanged
  • when encountering an error, one version throws an exception while the other one only writes an error message to the log or does nothing

As for any method ends with ? it indicates that a method returns a boolean value.

Understand basic OOP principles
Explain:
  • What's the difference between classes and objects?: An object is a unit of data. A class is what kind of data it is. every object has a unique object identifier. instance variables: variables with values that are unique to each class instance.
  • **Inheritance?:**is a fundamental principle of object-oriented programming. It allows a class to "inherit" (behavior or characteristics) of another, more general class. reference
  • Abstraction?: Abstraction means working with something we know how to use without knowing how it works internally.
  • Encapsulation?: Encapsulation is one of the main concepts in OOP. It is also called "information hiding". An object has to provide its users only with the essential information for manipulation, without the internal details.
  • Polymorphism?: Polymorphism allows treating objects of a derived class as objects of its base class. For example, big cats (base class) catch their prey (a method) in different ways. A Lion (derived class) sneaks on it, while a Cheetah (another derived class) simply outruns it. *So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types). For example, integers and floats are implicitly polymorphic since you can add, subtract, multiply and so on, irrespective of the fact that the types are different. They're rarely considered as objects in the usual term.
  • Distributed systems?
  • Hash tables?
  • Pointers?
  • ACID?
  • MVC?
  • Instantiation
  • Class: is a combination of state and methods that use the state.
  • Method
  • Function
  • Class variables and instance variables
  • Constructor: a special method associated with a class. The standard constractor is called new.
  • Superclass, base class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment