[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Stepping out on a Limb - some very ugly code

Sebastian Hungerecker

2/28/2007 8:04:00 PM

Am Mittwoch, 28. Februar 2007 20:52:44 schrieb Samantha:
> On 2/28/07, Samantha <rubygeekgirl@gmail.com> wrote:
> > On 2/28/07, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

> > > data=Hash.new
> > > ["street number","street name","city name","state","zip code"].each do
> > > |i|
> > > puts("Please enter your #{i}: ")
> > > data[i]=gets.chomp
> > > end

> Okay ... I wrote that up and it inputs things, but I have a few
> questions...
>
> 1) After getting the information from the user, I want to be able to output
> the information on the screen and get a confirmation. I'm assuming that I
> do that with:
>
> contact_info.each do |i|
> puts i
> end
>
> Yes? So... 1) How do I get this to be 'pretty' or formatted nicely like
> the original code, and 2) How do I get the state abbreviation as all
> upcase?

Just use contact_info["x"] to access information x (e.g. contact_info["street
name"] to access the street name), assuming contact_info is what you called
the hash (although I would recommend just calling it info, because that's less
to type ;-) )

--
Ist so, weil ist so
Bleibt so, weil war so

5 Answers

dblack

3/1/2007 3:24:00 AM

0

dblack

3/1/2007 11:57:00 AM

0

Max Muermann

3/1/2007 12:15:00 PM

0

On 3/1/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
<snip>>
> Lose the !. Just issue a new string with capitalize. There's no
> serious performance or memory issue here (unless someone has a name
> the size of "War and Peace" :-)

Even that should not be a problem:

> "War and Peace".size
#=> 13

;)

max

Louis J Scoras

3/1/2007 1:02:00 PM

0

On 2/28/07, Samantha <rubygeekgirl@gmail.com> wrote:
> After taking several suggestions, I've come up with 91 lines of code
> (including blank lines because I like organizing things).

> I'll post all 91 lines (including lines intentionally left blank), and
> explain what I'm having go wrong... I'm just not getting it, but maybe I
> don't quite understand certain things... (probably, not maybe ;) (And
> please excuse the rude prompts - I'm not really this rude, nor would I write
> such a rude program for deployment into the world.)

Samantha;

Cool script. I like the prompts ;)

One cool thing about ruby is that if you put key/value pairs at the
end of a method call they all get mashed together into a hash. You
can use that to help with readability. For example, many times in the
code you check a boolean condition and then write out a message. You
could try something like this:

def verify_info(ok, hash)
if ok
puts hash[:next]
else
puts hash[:fail]
exit 1
end
end

Now the messages can be passed in like so:

verify_info correct,
:next => "Good, now moving on, let's get a little information
about your skills.",
:fail => "Please come back when you have some goals."

Or even get rid of the "correct" variable:

verify_info boolean_prompt("Is this correct?"),
:next => "Great, let's move onto your experience.",
:fail => "Please come back when you know what you can do with
yourself. :)"

In this case it doesn't afford you too much, but it does get rid of
the repeated pattern. Say you wanted to change the exit code. Now
you'd only need to edit it in one place.



--
Lou.

James Gray

3/1/2007 1:08:00 PM

0

On Mar 1, 2007, at 7:02 AM, Louis J Scoras wrote:

> You could try something like this:
>
> def verify_info(ok, hash)
> if ok
> puts hash[:next]
> else
> puts hash[:fail]
> exit 1
> end
> end

If you keep doing a lot of this, you might want to look into the
HighLine library. It gives you the tools to validate and convert
input as well as issue error messages on certain conditions:

http://highline.ruby...

James Edward Gray II