From: Rob Sanheim Date: 2006-09-28T03:44:34+09:00 Subject: Re: Rails for the Rubyist On 9/27/06, Phrogz wrote: > Paul Lynch wrote: > > On 26 Sep 2006, at 00:10, Phrogz wrote: > > > > > What's the best source for the opposite? Learning Rails for those who > > > already know Ruby? I'm frustrated by the number of intro tutorials to > > > rails that have things like "I learned that @foo is called an > > > 'instance variable'". > [snip] > > Agile Web Development with Rails (2nd Ed) is going to be your answer, > > but I think you'd enjoy Ruby for Rails, too; I know I did. > > I just bought the PDF, and I'm not very far into it, but I've already > found this section: > > "In the description of views and controllers, we showed the controller > setting the > time to be displayed into an instance variable. The .rhtml file used > that instance > variable to substitute in the current time. But the instance data of > the controller > object is private to that object. How does ERb get hold of this private > data to > use in the template? > > The answer is both simple and subtle. Rails does some Ruby magic so > that > the instance variables of the controller object are injected into the > template > object. As a consequence, the view template can access any instance > variables > set in the controller as if they were its own. > > Some folks press the point: "just how do these variables get set?" > These folks > clearly don't believe in magic. Avoid spending Christmas with them." > > Very amusing, but exactly the sort of explanation and attitude that I > was hoping to avoid. Ah well; certainly other good information in it so > far. (I'm also discouraged by the appearance that the book takes the > track of "If you use an RDBMS other than MySQL, it'll mostly work, but > you're on your own when you run into trouble.") Regarding this particular magic, I'm pretty sure all Rails does is loop through all the instace variables on the controller, do a instance_variable_get to grab it and then an instance_variable_set to store it on the template object. So it just uses some the instance_variable* methods to bypass encapsulation and throw everything onto the template. - rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.com