Created
May 1, 2012 19:37
-
-
Save wyattdanger/2570770 to your computer and use it in GitHub Desktop.
Contrasting prototypal inheritance in JavaScript with classical inheritance in Ruby. In this gist you'll find a JavaScript file and a Ruby file which try to do the same thing, the outputs of each file, and a diff of the two outputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inspect = require('util').inspect, // To help with printing to console | |
log = console.log; | |
// Define class Foo | |
function Foo () { | |
this.a = []; | |
} | |
// Define class Bar which inherits from Foo | |
function Bar () {} | |
Bar.prototype = new Foo(); | |
Bar.prototype.constructor = Bar; | |
// Create instances of Bar | |
var baz = new Bar(), | |
qux = new Bar(); | |
log("New Objects:"); | |
log(' baz.a = %s', inspect(baz.a)); | |
log(' qux.a = %s', inspect(qux.a)); | |
// Push a value onto baz.a | |
baz.a.push(1); | |
log("After pushing 1 onto baz.a:"); | |
log(' baz.a = %s', inspect(baz.a)); | |
log(' qux.a = %s', inspect(qux.a)); | |
// Directly define baz.a | |
baz.a = [1,2,3]; | |
log("After directly defining baz.a as [1,2,3]:"); | |
log(' baz.a = %s', inspect(baz.a)); | |
log(' qux.a = %s', inspect(qux.a)); | |
// Remove baz.a | |
delete baz.a; | |
log("After removing a from baz:"); | |
log(' baz.a = %s', inspect(baz.a)); | |
log(' qux.a = %s', inspect(qux.a)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define class Foo | |
class Foo | |
attr_accessor :a | |
def initialize | |
@a = [] | |
end | |
end | |
# Define class Bar which inherits from Foo | |
class Bar < Foo | |
end | |
# Create instances of Bar | |
baz = Bar.new | |
qux = Bar.new | |
puts "New Objects:" | |
puts " baz.a = #{ baz.a }" | |
puts " qux.a = #{ qux.a }" | |
# Push a value onto baz.a | |
baz.a.push 1 | |
puts "After pushing 1 onto baz.a:" | |
puts " baz.a = #{ baz.a }" | |
puts " qux.a = #{ qux.a }" | |
# Directly define baz.a | |
baz.a = [1,2,3] | |
puts "After directly defining baz.a as [1,2,3]:" | |
puts " baz.a = #{ baz.a }" | |
puts " qux.a = #{ qux.a }" | |
# Remove baz.a | |
baz.send :remove_instance_variable, :@a | |
puts "After removing a from baz:" | |
puts " baz.a = #{ baz.a }" | |
puts " qux.a = #{ qux.a }" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New Objects: | |
baz.a = [] | |
qux.a = [] | |
After pushing 1 onto baz.a: | |
baz.a = [1] | |
qux.a = [1] | |
After directly defining baz.a as [1,2,3]: | |
baz.a = [1, 2, 3] | |
qux.a = [1] | |
After removing a from baz: | |
baz.a = [1] | |
qux.a = [1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New Objects: | |
baz.a = [] | |
qux.a = [] | |
After pushing 1 onto baz.a: | |
baz.a = [1] | |
qux.a = [] | |
After directly defining baz.a as [1,2,3]: | |
baz.a = [1, 2, 3] | |
qux.a = [] | |
After removing a from baz: | |
baz.a = nil | |
qux.a = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- ruby-output.txt | |
+++ javascript-output.txt | |
New Objects: | |
baz.a = [] | |
qux.a = [] | |
After pushing 1 onto baz.a: | |
baz.a = [1] | |
+ qux.a = [1] | |
- qux.a = [] | |
After directly defining baz.a as [1,2,3]: | |
baz.a = [1, 2, 3] | |
+ qux.a = [1] | |
- qux.a = [] | |
After removing a from baz: | |
+ baz.a = [1] | |
+ qux.a = [1] | |
- baz.a = nil | |
- qux.a = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment