SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 261 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


When you call a method, like some_array.size, Ruby looks for the size method in its method
tables for the class in question, and if Ruby finds the method, it executes the code that??™s associated
with it. If not, Ruby searches all the superclasses of that object until it finds one that has
a method of that name. If it can??™t find such a method, Ruby calls a special method called
method_missing and passes in the name it was trying to find and the arguments it was given.
Here??™s an example where we??™ve added method_missing to the String class, which will print
out a message to the user instead of raising an error:
class String
def method_missing method_name, *args
CHAPTER 5 ?–  BONUS FEATURES 112
puts "You tried to call #{name} and I don't know what that means!"
end
end
"Hello".foo_method
# prints "You tried to call foo_method and I don't know what that means!"
We can use the behavior of method_missing to our advantage, because we can make an
object that will use the method names passed to it through method_missing and build our SQL,
for example:
module RQuery
class Conditions
def initialize(&blk)
@columns = []
blk.


Pages:
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6