join("..", "test.rb") # => "../test.rb"
??? File.open can take a block, which will automatically close the file when exited.
Hash
??? Hash.new accepts a block, which provides a way to calculate a default value if the hash
has none. This is useful for caching. The first time a cached method is called with a
particular set of arguments, the block is invoked; it calculates the value and stores it
in the hash for future access. ActiveSupport has an implementation of hash caching in
caching_tools.rb, which generates hashes like this:
Hash.new do |as, a|
as[a] = Hash.new do |bs, b|
* Named after Randal Schwartz, who popularized the map-sort-unmap technique in Perl.
Kernel
48 | Chapter 2: ActiveSupport and RailTies
bs[b] = slow_method(a, b)
end
end
??? Hash#delete removes a value from the hash and returns it. This is useful for stripping
out keyword arguments from a hash before passing it along somewhere.
Kernel
??? Kernel#Array tries to coerce its argument into an array:
Array([1,2,3]) # => [1, 2, 3]
Array(1..3) # => [1, 2, 3]
Array(1) # => [1]
Module
??? Module#remove_method removes a method from the specified class. Module#undef_method,
on the other hand, actively prevents that method from being invoked on the class; it
inserts a special entry into the m_tbl that stops method lookup.
Proc
??? Proc#[] is shorthand for Proc.call.
p = lambda{|x| x * 2}
p[3] # => 6
String
??? String#%(args) interpolates the arguments into itself in the manner of sprintf.
Pages:
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84