On Thu, Nov 10, 2011 at 21:38, Darren H. [email protected] wrote:
the price is given in the csv file when I run the code I get an error
"syntax error, unexpected keyword_do_block do (qty * price)
Does the code that gives you the error, look like the above, though
minus the quote markers of course? I very much doubt it, since at the
very least the puts of the quantity prompt, is not syntactically
valid. If you try to retype, you’re bound to get something wrong,
assuming you are in fact only human. Make the machine do the
work, and use copy-and-paste.
Now, assuming it at least resembles the above, you’ve got something
weird going on in the stuff you tell it to do in case of a match.
What is “do (price * qty)” supposed to mean? At that point, the user
hasn’t even entered a quantity, so you’ll probably wind up with it
gritching about an undefined variable, or if it somehow defaults to
zero the math result will always be zero, or if its value is leftover
from a previous run then it’s likely to be wrong for this one.
As for the unexpected keyword, I think what it’s probably trying to
tell you is that it doesn’t expect a “do” at that point. A “do” is
usually for passing a block of code to some method such as map or
times, to tell it what to map onto each item or what to do a given
number of times. But you don’t have any such construct preceding your
Since I’m not sure exactly what you’re trying to accomplish and how
(though I think I could make a good guess), here’s what I suggest.
Start with plain English (or whatever). Write out how you would tell
a human to do what you’re asking. Do it in very simple steps, one
by one. Indent sub-steps, ending indented blocks with “end” or some
such placeholder just to be sure. (I’m going to use dash-space pairs
just so they don’t get squished out in the web forum, but “for real”
I’d use just spaces.) Then translate each step into Ruby. If you
find that something doesn’t translate well, you probably need to break
it down into simpler steps. (These can be either put where they are
in the list, or as a new method.) This is what we call “pseudocode”.
Producing these thoughts, if not necessarily this writing, is in
fact the core essential of old-style programming, what the fancypants
call “top-down stepwise refinement”. It’s also the core of modern
programming once you get past the overall system design of objects and
suchlike, certainly applicable at the level of individual methods.
Once you get very well versed in this sort of thing, you can skip this
step entirely and spit out code directly.
In the case of what I think you’re trying to do, it might start like:
- Ask me for a Product ID Number
- Write down what I tell you, and mark it as ID
- Look up the information about the product with that ID
- Ask me for a Quantity
- Write down what I tell you, and mark it as Quantity
- Now what do we do? (The code above stops there.)
Then “Look up the information about the product with that ID” might
then get broken up into:
- For each line in the CSV file
- If the first part of the line matches the ID you wrote down
- This seems to be the part where you’re not sure what to do, right?
Can you take it from there?