Skip to content

Instantly share code, notes, and snippets.

@stonegao
Created January 13, 2010 16:54
Show Gist options
  • Save stonegao/276364 to your computer and use it in GitHub Desktop.
Save stonegao/276364 to your computer and use it in GitHub Desktop.
drew.send(:battle_cry)
# => Drew says zing!!!
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!!
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
drew['battleCry']();
// => Drew says zing!!!
def drew.throw_star
puts "throwing a star"
end
drew.throw_star
# => throwing a star
drew.throwStar = function () {
puts("throwing a star");
}
drew.throwStar();
// => throwing a star
class Ninja
def battle_cry
puts "#{name} says zing!!!"
end
end
drew.battle_cry
# => Drew says zing!!!
adam.battle_cry
# => Adam says zing!!!
Ninja.prototype.battleCry = function () {
puts(this.name + " says zing!!!");
}
drew.battleCry();
// => Drew says zing!!!
adam.battleCry();
// => Adam says zing!!!
var puts = require('sys').puts;
function Ninja(name) {
this.name = name;
}
var drew = new Ninja("Drew");
var adam = new Ninja("Adam");
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