Created
January 13, 2010 16:54
-
-
Save stonegao/276364 to your computer and use it in GitHub Desktop.
This file contains 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
drew.send(:battle_cry) | |
# => Drew says zing!!! |
This file contains 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
class Object | |
def metaclass | |
class << self; self; end | |
end | |
end | |
sword_symbol = "*********" | |
drew.metaclass.send(:define_method, 'swing') do |sound_effect| | |
puts "#{name}: #{sword_symbol} #{sound_effect}" | |
end | |
drew.swing 'slash!!' | |
# => Drew: ********* slash!! |
This file contains 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
color_name = 'black' | |
Ninja.send(:define_method, 'color') do | |
puts "#{name}'s color is #{color_name}" | |
end | |
drew.color | |
# => Drew's color is black | |
adam.color | |
# => Adam's color is black |
This file contains 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
drew['battleCry'](); | |
// => Drew says zing!!! |
This file contains 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
def drew.throw_star | |
puts "throwing a star" | |
end | |
drew.throw_star | |
# => throwing a star |
This file contains 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
drew.throwStar = function () { | |
puts("throwing a star"); | |
} | |
drew.throwStar(); | |
// => throwing a star |
This file contains 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
class Ninja | |
def battle_cry | |
puts "#{name} says zing!!!" | |
end | |
end | |
drew.battle_cry | |
# => Drew says zing!!! | |
adam.battle_cry | |
# => Adam says zing!!! |
This file contains 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
Ninja.prototype.battleCry = function () { | |
puts(this.name + " says zing!!!"); | |
} | |
drew.battleCry(); | |
// => Drew says zing!!! | |
adam.battleCry(); | |
// => Adam says zing!!! |
This file contains 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 puts = require('sys').puts; | |
function Ninja(name) { | |
this.name = name; | |
} | |
var drew = new Ninja("Drew"); | |
var adam = new Ninja("Adam"); |
This file contains 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
class Ninja | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
drew = Ninja.new("Drew") | |
adam = Ninja.new("Adam") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment