Prev | Current Page 106 | Next

Brad Ediger

"Advanced Rails"

It
is usually used to encapsulate some incidental operations on an object in a block:
returning(Person.new) do |p|
p.name = "Brad"
end # => #
??? Object#with_options provides a way to factor out redundant options on multiple
method calls. It is usually seen in routing code:
map.with_options(:controller => "person") do |person|
person.default "", :action => "index"
person.details "/details/:id", :action => "details"
end
The with_options call yields an OptionMerger, which is a proxy that forwards all of its
method calls to the context (which, in this example, is the original map). The options
provided are merged into the method call??™s last hash argument, so the effect of the
person.default call is the same as:
map.default "", :action => "index", :controller => "person"
map.details "/details/:id", :action => "details", :controller => "person"
There is no magic here that makes this specific to Rails routes; you can use this with
any method that takes a hash of Rails-style keyword arguments as the last parameter.
The proxy object passed into the with_options block delegates any unknown method
calls to the target.
Range core_ext/range/include_range.rb, core_ext/range/overlaps.rb
??? Range#include? checks to see whether a range completely includes another range:
(1..10).include?(3..5) # => true
(1..10).include?(3..15) # => false
??? Range#overlaps? checks to see whether a range overlaps another range:
>> (1.


Pages:
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118