How about param_name instead of params[:name]

How about doing for params within ApplicationController what
ActiveRecord has with find_by_column_name :

module ActionController
class Base
def method_missing(method_id, args, &block)
if method_id.to_s =~ /^param_(.
)/
params[$1.intern]
else
super
end
end
end
end

Then, instead of

params[:first_name]
params[:amount]

write

param_first_name
param_amount

and so on. Seems a lot easier to write and read.

Stephan

Neat trick, but I think it’s harder to read. My editor easily identifies
hashes / symbols, etc.
I’d prefer something like

params.id
params.name

or

params.user.id
params.user.first_name

instead of

params[:user][:first_name]

:slight_smile:

On Nov 29, 2007 10:54 PM, Stephan W.
[email protected]

Brian H. wrote:

I’d prefer something like

params.id
params.name

or

params.user.id
params.user.first_name

Yes that is more uniform – I forgot about the nested hashes.

Stephan

What you’re asking for is really quite simple and shouldn’t slow you
down. However, it’ll get confused when you have a word set as a key as
both a string and a symbol, but you should avoid that anyways.

http://pastie.caboo.se/123603

Good luck!

On Nov 30, 12:11 am, Stephan W. [email protected]

Brian H. wrote:

Neat trick, but I think it’s harder to read. My editor easily identifies
hashes / symbols, etc.
I’d prefer something like

params.id
params.name

or

params.user.id
params.user.first_name

instead of

params[:user][:first_name]

Actually, another twist is that names which are params object instance
methods (such as to_s) are not going to work with this.

params.to_s -> ??
params.display -> ??
params.dup -> ??

Unpleasant surprises seem unavoidable.

So maybe the whole thing is not such a good idea after all. Underscore
doesn’t work, period doesn’t work.

Stephan

Well, no… that’s why you’re using method_missing. to_s is defined, so
method_missing doesn’t get fired.

But I do think it’s really more of a waste of time :slight_smile:

On Nov 30, 2007 1:14 AM, Stephan W.
[email protected]

Brian H. wrote:

Well, no… that’s why you’re using method_missing. to_s is defined, so
method_missing doesn’t get fired.

What I was thinking of is when method_missing is not invoked for “to_s”
(and the other examples), then the value produced would be different
from params[:to_s] (in most cases). In other words, the “interface” is
not uniform.

Programmers would need to avoid certain names when using params.name,
unlike when one just uses the existing params[:name].

Stephan

Michael B. wrote:

You guys are very right, and this is more for just-so-you-know than
actuall use in a production environment, but you could use it if your
hash doesnt have a lot of keys. How many of you have had a hash with a
key of to_a?

It’s not only to_a, to_s: Hash objects have a lot of methods, for
example:

clear, collect, default, display, entries, index, shift, select,
store, size, sort, type, update, values

Even Object method names make good form parameter names, such as

inspect, send, id, freeze, extend, display, method, type

For some application these names would be convenient to use in this
other way.

Alternatively, you could do something else, which I’ll
have up in a few.

Sounds good! In the mean time, I’m thinking double-underscores might do
the trick as in param__user__id for params[:user][:id] (3 characters
shorter).

Stephan

It’s a fun excercise but I don’t think what’s there is really that hard
to
read or use. I’m just interested in this for the academic side of
things.
It’s neat to see what Ruby can do.

On Nov 30, 2007 12:25 PM, Stephan W.
[email protected]

You guys are very right, and this is more for just-so-you-know than
actuall use in a production environment, but you could use it if your
hash doesnt have a lot of keys. How many of you have had a hash with a
key of to_a? Alternatively, you could do something else, which I’ll
have up in a few.

It’s really simple to add extra “characters” to make sure there’s no
overlapping. For example, “hash.key.user.id” could be done quite
easily, adapting from my code earlier. I’ll have that up in a moment.

It’s really simple to add extra “characters” to make sure there’s no
overlapping. For example, “hash.key.user.id” could be done quite
easily, adapting from my code earlier. I’ll have that up in a moment.

Here: Parked at Loopia

The method “key” just returns the hash, then the code from before
kicks in. You can change key to whatever you want to make it better
for you.