From: "Ara.T.Howard" Date: 2005-09-15T12:21:35+09:00 Subject: Re: params v.s. @params in rails? --8323328-1532006418-1126753957=:15399 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1532006418-1126753957=:15399" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-1532006418-1126753957=:15399 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Thu, 15 Sep 2005, Ron Jeffries wrote: > On Mon, 5 Sep 2005 01:26:44 +0900, David Heinemeier Hansson > wrote: > >>> The recommendation for using the params method, instead of accessing >>> the @params instance variable directly, is that it=B4s more uniform to >>> use the accessor method, since you can also do that from the view. >> >> Direct access to these instance variables is deprecated. The same goes >> for cookies, session, request, response, and the other accessors. Use >> the accessor instead of going directly, so it's request.get? instead >> of @request.get?. > > David, Chet Hendrickson and I are just getting back to Ruby after too lon= g away, > and we've just started working through the Rails book. Mostly happy so fa= r, > though we're just a couple of days in. > > One thing that confused me was the validate method in the first example. = I'm > sure I'm missing something really obvious, but I'd rather be embarrassed = than > confused. ;-> The validate is: > > def validate > errors.add(:price, "should be positive") unless price.nil? || price >= =3D 0 > end > > I don't see how "price" canrefer to the field in the record, unless price > isperhaps an accessor kind of method. But in the class definition, I don'= t see > any declaration of a price method. runtime and compile time aren't that different in ruby: harp:~ > cat a.rb class Row < ::Array FIELDS =3D %w( product price ) def method_missing(m, *a, &b) idx =3D FIELDS.index m.to_s return self[idx] if idx super end end row =3D Row[ 'foobar', '17 bucks' ] p row.price harp:~ > ruby a.rb "17 bucks" this routes through method_missing each time, which is expensive, but you c= an define the method at that point so future calls don't: harp:~ > cat a.rb class Row < ::Array FIELDS =3D %w( product price ) def method_missing(m, *a, &b) idx =3D FIELDS.index m.to_s if idx puts 'defining the method once...' self::class.class_eval <<-definition def #{ m } self[ #{ idx } ] end definition send m else super end end end row =3D Row[ 'foobar', '17 bucks' ] 2.times{ p row.price } harp:~ > ruby a.rb defining the method once... "17 bucks" "17 bucks" it's precisely these sorts of possibilties which makes ruby such an attract= ive means of writing code with dynamic responsibilies and requirements.. cheers. -a --=20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-1532006418-1126753957=:15399-- --8323328-1532006418-1126753957=:15399--