[someone else (apologies to now anonymous author) wrote:]
PROMPTS = [“First Name”, “Last Name”, “Phone”]
first_name,last_name,phone = PROMPTS.map { |prompt | prompt_and_read(
prompt ) }
[snip]
first_name = prompt_and_read “First name”
last_name = prompt_and_read “Last name”
phone = prompt_and_read “Phone”
[snip[
The iterator example reads like perl to me, which in and of itself
isn’t bad, but it just seems like using a ruby construct for the sake
of using a ruby construct.
That the iterator example reads like Perl is not necessarily a Bad
Thing™. That the example shown doesn’t really demonstrate how this
makes your life better is. To me the problem is not the iterator so much
as the parallel assignment-- if the input was a list, let’s keep the
output as a list (or a hash)
I’m new to Ruby (so apologies for sounding ignorant) but doesn’t the
Ruby example, also show how you might go about doing
Internationalization.
To truly do internationalization you would probably want to build a class
that stored your various phrases and allowed you to set a global or class
variable to the language to be used, then whenever you need a string you
call a method of that class, which responds appropriately.
This way you can set up things like handling plurals or other grammatical
constructs. This is the function of the GNU gettext library, and the RAA
appears to have some similar stuff. Disclaimer: I’ve never used any of
gettext or the RAA modules-- I have only read about them. But thank you
for reminding me that it’s time to include this in an incipient
application I’m writing-- looks like I’ll do more than just reading soon.
Yes it makes it less clear, but it means that it becomes a simple case
of swapping that array PROMPTS for another with say French words and
leaving the rest of the code intact.
Where as the other example would leave you having to track down each of
the quoted strings and changing them.
Except that for the contrived example shown, neither of the above seem that
efficient (in terms of programmer brain and finger usage) to me.
Why not create a list of parameters and use it to build a hash, rather than
an enumerated set of locals?
PROMPTS = [“First Name”, “Last Name”, “Phone”]
hv = {}
PROMPTS.each{|p| hv[p] = prompt_and_read(p)}
Now I have a data structure that is still easy to use, but also
self-contained. What if I want to pass all the results here to another
routine for validation or processing? Whether in the first example or the
second I still have to now script:
validate_and_process(“First Name”, first_name)
validate_and_process(“Last Name”, last_name)
validate_and_process(“Phone”, phone)
That’s a lot of work compared to:
hv.each_key{|k| validate(k, hv[k])}
Or, if we write our validate_and_process method so it can accept a hash:
validate_and_process(hv)
This way if I need to add in elements, doing so requires that I add those
elements in one place only (although my example validate_and_process
method might need to add code to handle new additions, that’s actual new
program logic, not just more code that all does the same thing).
-michael
···
On Wednesday 21 August 2002 05:52, Rob McG wrote:
++++++++++++++++++++++++++++++++++++++++++
Michael C. Libby x@ichimunki.com
public key: http://www.ichimunki.com/public_key.txt
web site: http://www.ichimunki.com
++++++++++++++++++++++++++++++++++++++++++