Divya Khullar a écrit :
> Hello I am new to rails. I was going through the gallery example in
> Ajax on Rails book and i came accorss this code. It beats me what this
> means. Please can one of you Rails Guru help me
That's meta-programming: this code build fragments of Ruby code that
then get eval'd in the context of the current class. In short, it
synthesizes new methods for the current class, on the fly.
> %w(full thumb medium).each do |size|
OK, so we'll create code fragments for three methods: 'full', 'thumb'
and 'medium' (%w produces a string array from its argument).
> class_eval <<-END
Everything else until we meet 'END,' possibly indented, is a string
literal that will end up being passed to class_eval.
> def #{size}
This code defines a method named after the current value of /size/ (e.g.
'full').
> find_photo
> send_data @photo.#{size}, :filename =>
> "[EMAIL PROTECTED]", :type => 'image/jpeg', :disposition =>
> 'inline'
The method will simply grab the picture object, and send the binary
contents of the proper field of it (@photo is an object found by
find_photo, and #{size} will be expanded in your Ruby code string to the
current value of size, as before on the /def/ line.
> end
> caches_page :#{size}
Let's cache this result for performance
> END
End of the Ruby code string: class_eval gets called and turns this Ruby
into reality.
> end
End of the each iteration. You just generated three new methods!
'HTH
--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---