Skip to content

Instantly share code, notes, and snippets.

@imtoanle
Created April 23, 2015 08:46
Show Gist options
  • Save imtoanle/32eb40c2e4d16c5ac56c to your computer and use it in GitHub Desktop.
Save imtoanle/32eb40c2e4d16c5ac56c to your computer and use it in GitHub Desktop.
Ruby Interview Question
## Begin!
Senior programmers won't have a problem with these, while junior programmers will usually give only half-answers.
#### What is a class?
A text-book answer: classes are a blue-print for constructing computer models for real or virtual objects... boring.
In reality: classes hold **data**, have **methods** that interact with that data, and are used to **instantiate objects**.
Like this.
```ruby
class WhatAreClasses
def initialize
@data = "I'm instance data of this object. Hello."
end
def method
puts @data.gsub("instance", "altered")
end
end
object = WhatAreClasses.new
object.method
#=> I'm altered data of this object. Hello.
```
#### What is an object?
An instance of a class.
To some, it's also the root class in ruby (Object).
Classes themselves descend from the Object root class. (Kudos to Ezra)
#### What is a module? Can you tell me the difference between classes and modules?
Modules serve as a mechanism for **namespaces**.
```ruby
module ANamespace
class AClass
def initialize
puts "Another object, coming right up!"
end
end
end
ANamespace::AClass.new
#=> Another object, coming right up!
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment