[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how remove puts inside method?

Kai Vermehr

1/5/2006 4:00:00 PM

I have a method (process_transaction) that uses puts to output a result.
Now I wanted to remove puts, so I could further process the result of
the method before printing it. But I can't get it to work ...

# method checks what key matches with string
# and adds additional info to each string def process_transaction
(line_of_file)

if get_money(line_of_file) < 0

Ausgabekonten.any? { |key, konto|
if key =~ line_of_file
kontonummer = extract_four_digit(konto)
puts line_of_file.chomp + " \"" + key.to_s + "\" " +
kontonummer.to_s + " 1200" + " AUSGABE"
break true
end
} or warn line_of_file.chomp + " (no key) \"\" \"\" \"\"
AUSGABE_NO_KEY"

else

Einnahmekonten.any? { |key, konto|
if key =~ line_of_file
kontonummer = extract_four_digit(konto)
puts line_of_file.chomp + " \"" + key.to_s + "\"" + " 1200 " +
kontonummer.to_s + " EINNAHME"
break true
end
} or warn line_of_file.chomp + " (no key) \"\" \"\" \"\"
EINNAHME_NO_KEY"

end

end

# apply process_transaction to each line of the transactions array

transactions.each do |member|
process_transaction(member)
end

--
Posted via http://www.ruby-....


13 Answers

Kai Vermehr

1/5/2006 4:06:00 PM

0

For example I would like to remove puts from the process_transaction
method and the do this:

transactions.each do |member|
puts process_transaction(member)
end

(sorry for the messy line breaks!)

--
Posted via http://www.ruby-....


Robert Klemme

1/5/2006 4:12:00 PM

0

Carlos Delmar wrote:
> For example I would like to remove puts from the process_transaction
> method and the do this:
>
> transactions.each do |member|
> puts process_transaction(member)
> end
>
> (sorry for the messy line breaks!)

I'm not really sure what you're up to. Can you elaborate? The only thing
that comes to mind is that you probably want to use #each instead of #any?
because the latter is merely for identifying something and it does short
circuit (i.e. stop once the first block returns true).

Kind regards

robert

Kevin Olbrich

1/5/2006 4:21:00 PM

0

You could just mash the output into one long string...

output = ""
...
output << line_of_file.....
...
return output

Then return the string at the end of the function.
You may need to tack on some linefeeds at the end of each line.

--
Posted via http://www.ruby-....


Ara.T.Howard

1/5/2006 4:21:00 PM

0

Kai Vermehr

1/5/2006 8:14:00 PM

0

> I'm not really sure what you're up to. Can you elaborate?

I'd like the output of process_transaction as a long string. But I don't
completely understand the post from Kevin:

> output = ""
> ...
> output << line_of_file.....
> ...
> return output

where does this go?

If the method works I would like to apply it to each line of
transactions array and output the result to a new file.

here's the latest version of the script:
http://rafb.net/paste/results/D63...


--
Posted via http://www.ruby-....


Robert Klemme

1/6/2006 8:45:00 AM

0

Carlos Delmar wrote:
>> I'm not really sure what you're up to. Can you elaborate?
>
> I'd like the output of process_transaction as a long string. But I
> don't completely understand the post from Kevin:
>
>> output = ""
>> ...
>> output << line_of_file.....
>> ...
>> return output
>
> where does this go?
>
> If the method works I would like to apply it to each line of
> transactions array and output the result to a new file.
>
> here's the latest version of the script:
> http://rafb.net/paste/results/D63...

The parts after "or" will never be called because each returns the
Enumerable. You have several options; one of is replacing "puts" by
"return" and remobing "or". In the calling block you can then process the
string return value as needed. For example you can do

puts transactions.map do |member|
process_transaction(member)
end

This will print each result on a single line. Or you can do

puts transactions.map do |member|
process_transaction(member)
end.join

to get a single long string.

There are lots of alternatives...

Kind regards

robert

Kai Vermehr

1/6/2006 12:06:00 PM

0

thanks Robert ... but it did not work out ...

>The parts after "or" will never be called because each returns the Enumerable.

that was right, i replaced 'puts' after 'or' with 'warn' (which I had
removed by mistake)

Fnd a working script with some data here:

http://rafb.net/paste/results/2yJ...

BUT: I still would like to remove 'puts' and 'warn' in the
process_transaction method and would much prefer to 'puts' a resulting
string later in the transactions.each statement (or write that to a
file).

--
Posted via http://www.ruby-....


Lyndon Samson

1/6/2006 12:31:00 PM

0

>
> BUT: I still would like to remove 'puts' and 'warn' in the
> process_transaction method and would much prefer to 'puts' a resulting
> string later in the transactions.each statement (or write that to a
> file).


A simple solution is you can allways stuff your output in an array.

out = []

out << "Message #{msg}"

out.each { |line| puts line }

You know you can return multiple values from a method right?

return out, retCode, a,b,c

Kevin Olbrich

1/6/2006 12:44:00 PM

0

Carlos Delmar wrote:
>> I'm not really sure what you're up to. Can you elaborate?
>
> I'd like the output of process_transaction as a long string. But I don't
> completely understand the post from Kevin:
>
>> output = ""
>> ...
>> output << line_of_file.....
>> ...
>> return output
>
> where does this go?
>
> If the method works I would like to apply it to each line of
> transactions array and output the result to a new file.
>
> here's the latest version of the script:
> http://rafb.net/paste/results/D63...

Just put this type of structure in whichever function you are trying to
capture the output of.

first line creates a string object, then you use the '<<' to append
output to that string, then you do something with the string when you
are done.

--
Posted via http://www.ruby-....


Kai Vermehr

1/6/2006 2:07:00 PM

0

thanks a lot, now it works: http://rafb.net/paste/results/8Fs...

still I don't really understand what the ouput variable does. And why
simply removing the output statements does not work:
http://rafb.net/paste/results/hjO...

:/?


--
Posted via http://www.ruby-....