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 [code] %w(full thumb medium).each do |size| class_eval <<-END def #{size} find_photo send_data @photo.#{size}, :filename => "\#{@photo.id}.#{size}.jpg", :type => ''image/jpeg'', :disposition => ''inline'' end caches_page :#{size} END end [code] --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 meThat''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 <<-ENDEverything 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 => > "\#{@photo.id}.#{size}.jpg", :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> ENDEnd of the Ruby code string: class_eval gets called and turns this Ruby into reality.> endEnd 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: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks a lot Christophe. This was very helpfull. Thank you. On Apr 7, 3:12 am, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> 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 => > > "\#...@photo.id}.#{size}.jpg", :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: t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---