Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active August 29, 2015 14:16
Show Gist options
  • Save epitron/7885de5f7105977fb5b5 to your computer and use it in GitHub Desktop.
Save epitron/7885de5f7105977fb5b5 to your computer and use it in GitHub Desktop.
Objects have methods and instance variables
A class, and its instances, are Objects
A class is an Object that can create other Objects
An instance is an object that holds data and lets you run methods on it.
self refers to the object in which your code is currently executing
(ie: in an instance method, "self" is the instance
in a class, "self" is the class,
outside a class, "self" is "Object")
class Object
self #=> Object
class Person
self #=> Person
# this class creates Person instances
def initialize(name)
@name = name
end
# Calling Person.new generates a new object, and attaches the instance methods to it.
def an_instance_method
self #=> instance of Person (eg: Person.new("bob"))
end
end
end
// Define a class
Person = function(name) {
// this is like ruby's "initialize"
this.name = name
this.age = Math.random()*100
}
// A variable on the class object
Person.people = []
/////////////////////////////////////
// Class Methods
Person.all = function() {
return this.people
}
Person.first = function() {
return this.all()[0]
}
Person.add = function(name) {
person = new Person(name)
this.people.push(person)
return person
}
Person.add_random_people = function(n) {
for (var i = 0; i < n; i++) {
this.add("Random#{n}")
}
}
/////////////////////////////////////
// Instance Methods
Person.prototype.name_and_age = function() {
return "name: " + this.name + ", age: " + this.age
}
Person.prototype.get_older = function() {
this.age += 1
}
/////////////////////////////////////
// Test it out
bob = Person.add("bob")
console.log(bob)
Person.add("alice")
Person.add("ZUUL")
Person.add_random_people(3)
console.log(Person.all())
console.log(Person.first())
console.log(bob.name_and_age())
bob.get_older()
console.log(bob.name_and_age())
// Proof that you can't call a prototype method directly!
console.log(Person.prototype.get_older())
console.log(Person.prototype.name_and_age())
# Define a class
class Person
# A variable on the class object
@people = []
###################################
## Class Methods
def self.all
@people
end
def self.add(name)
person = Person.new(name)
@people << person
end
def self.first
@people.first
end
def self.add_random_people(n)
(1..n).each {|n| add("Random#{n}") }
end
###################################
## Instance Methods
attr_accessor :name, :age
def initialize(name)
@name = name
@age = rand(100)
end
def name_and_age
"name: #{name}, age: #{age}"
end
def get_older
@age += 1
end
end
###################################
## Test it out
bob = Person.add("bob")
puts bob
Person.add("alice")
Person.add("ZUUL")
Person.add_random_people(3)
puts Person.all
puts Person.first
puts bob.name_and_age
bob.get_older
puts bob.name_and_age
# Proof that you can't call an instance method on a class directly!
Person.get_older
Person.name_and_age
@fightingtheboss
Copy link

Very nice comparison! Really useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment