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
# The following fails with a failure to call "x=" private method | |
String.send(:attr_accessor, :x) | |
s = "" | |
s.x = 100 | |
# The following works: | |
class String | |
self.send(:attr_accessor, :x) | |
end | |
s = "" |
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
>> module M | |
>> x = 100 | |
>> define_method(:x=) {|v| x = v } | |
>> define_method(:x) { x } | |
>> end | |
=> #<Proc:0x0000010102dfd0@(irb):4 (lambda)> | |
>> class A | |
>> include M | |
>> end | |
=> A |
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
require 'open-uri' | |
url = 'http://www.hobotraveler.com/uploaded_images/207-249-tiger-nut-efio-togo-food-798405.jpg' | |
f = File.open('image.jpg', 'w') | |
open(url) { |io| f.puts(io.read) } | |
f.close |
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 m1 | |
commands = {:exit => Proc.new { break }} | |
loop do | |
commands[:exit].call | |
end | |
puts "Done!" | |
end | |
m1 |
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
ruby-1.9.2-head :001 > module M1 | |
ruby-1.9.2-head :002?> def self.m1 | |
ruby-1.9.2-head :003?> "self.m1 in M1" | |
ruby-1.9.2-head :004?> end | |
ruby-1.9.2-head :005?> end | |
=> nil | |
ruby-1.9.2-head :006 > module M2 | |
ruby-1.9.2-head :007?> include M1 | |
ruby-1.9.2-head :008?> def self.m2 | |
ruby-1.9.2-head :009?> m1 |
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
module M | |
def self.included(base) | |
base.class_eval do | |
singleton_class = (class << self; self; end) | |
singleton_class.send(:define_method, :foo) do | |
puts "in foo! base is #{base}" | |
end | |
end | |
end | |
end |
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 C | |
def initialize(*args) | |
puts "hi from C" | |
end | |
end | |
module M | |
def self.included(clazz) | |
clazz.class_eval do | |
alias_method :old_initialize, :initialize |
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
ruby-1.9.2-head :001 > class C | |
ruby-1.9.2-head :002?> def ==(o) | |
ruby-1.9.2-head :003?> puts "==(0)" | |
ruby-1.9.2-head :004?> super | |
ruby-1.9.2-head :005?> end | |
ruby-1.9.2-head :006?> def eql?(o) | |
ruby-1.9.2-head :007?> puts "eql?(o)" | |
ruby-1.9.2-head :008?> super | |
ruby-1.9.2-head :009?> end | |
ruby-1.9.2-head :010?> def hash |
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
module M | |
def m1 | |
puts "hi from M" | |
end | |
def handle(&block) | |
block.call | |
end | |
def handle2(&block) |
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
dict = {} | |
'aardvark'.chars.inject(dict) { |dict,c| dict[c] = {} } | |
def find(key, dict) | |
current_dict = dict | |
key.chars.each do |c| | |
return nil unless current_dict.has_key?(c) | |
current_dict = current_dict[c] | |
end | |
end |
OlderNewer