[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

my stupid code ...

vnpenguin

12/31/2005 9:24:00 PM

Hi,
I'm trying to write a small ruby script which accepts:
1. Input : File or $stdin
2. Output : File or $stdout

This is my stupid code:
#---------------------------------------------------------
def do_some_thing(str)
# do some things over str
end

f_in = ARGV[0]
f_out = ARGV[1]
if f_out==nil
fout = File.open(f_out,"w")
end

if f_in==nil
$stdin.each { |line|
if f_out==nil
print do_some_thing(line)
else
fout.print do_some_thing(line)
end
}
else
File.open(f_in,"r").each {|line|
if f_out==nil
print do_some_thing(line)
else
fout.print do_some_thing(line)
end
}
end
fout.close if f_out!=nil
#--------------------------------------------------------------------
Don't laugh at me :-) I'm learning Ruby so I would like to hear from
you a better way to write this small code.

Thank you in advance and Happy New Year !

8 Answers

Erik Veenstra

12/31/2005 9:58:00 PM

0

> I'm trying to write a small ruby script which accepts:
>
> 1. Input : File or $stdin
> 2. Output : File or $stdout

Abstraction is the keyword. Put the decision to open a file or
stdin or stdout in a a method on the class File and put that
code in a library. That keeps your application clean and
simple.

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

# LIBRARY

class File
def self.open_std(file, mode="r", *rest, &block)
if file.nil?
if block_given?
block.call(mode.include?("r") ? $stdin : $stdout)
else
mode.include?("r") ? $stdin : $stdout
end
else
File.open(file, mode, *rest, &block)
end
end
end

----------------------------------------------------------------

# APPLICATION

require "your_library"

def do_some_thing(str)
# do some things over str
str.upcase
end

File.open_std(ARGV.shift, "r") do |f_in|
File.open_std(ARGV.shift, "w") do |f_out|
f_in.each do |line|
f_out.puts do_some_thing(line)
end
end
end

----------------------------------------------------------------

Andreas S.

12/31/2005 10:06:00 PM

0

unknown wrote:
> Hi,
> I'm trying to write a small ruby script which accepts:
> 1. Input : File or $stdin
> 2. Output : File or $stdout
>
> This is my stupid code:
> #---------------------------------------------------------
> def do_some_thing(str)
> # do some things over str
> end
>
> f_in = ARGV[0]
> f_out = ARGV[1]
> if f_out==nil
> fout = File.open(f_out,"w")
> end
>
> if f_in==nil
> $stdin.each { |line|
> if f_out==nil
> print do_some_thing(line)
> else
> fout.print do_some_thing(line)
> end
> }
> else
> File.open(f_in,"r").each {|line|
> if f_out==nil
> print do_some_thing(line)
> else
> fout.print do_some_thing(line)
> end
> }
> end
> fout.close if f_out!=nil

fin = ARGV[0] ? File.open(ARGV[0],'r') : $stdin
fout = ARGV[1] ? File.open(ARGV[1], 'w') : $stdin
fin.each_line do |line|
fout.print do_some_thing(line)
end

Or (hides File.open errors):
fin = File.open(ARGV[0],'r') rescue $stdin
fout = File.open(ARGV[1], 'w') rescue $stdout
fin.each_line do |line|
fout.print do_some_thing(line)
end

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


Andreas S.

12/31/2005 10:10:00 PM

0

Erik Veenstra wrote:
>> I'm trying to write a small ruby script which accepts:
>>
>> 1. Input : File or $stdin
>> 2. Output : File or $stdout
>
> Abstraction is the keyword. Put the decision to open a file or
> stdin or stdout in a a method on the class File and put that
> code in a library. That keeps your application clean and
> simple.

I do not think that tinkering with the core classes is a good idea for
problems as simple as this.

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


Erik Veenstra

12/31/2005 10:25:00 PM

0

> fin = File.open(ARGV[0],'r') rescue $stdin
> fout = File.open(ARGV[1], 'w') rescue $stdout
> fin.each_line do |line|
> fout.print do_some_thing(line)
> end

You should close an IO stream, when it is a file.

gegroet,
Erik V. - http://www.erikve...

Erik Veenstra

12/31/2005 10:33:00 PM

0

> I do not think that tinkering with the core classes is a good
> idea for problems as simple as this.

Make it a new class, instead of "reusing" a core class... :)

The point is that the question "use a file or std stream?" is
asked so often, that it's worth pushing the code down into the
language.

"Enrich the language, so the applications are small."

gegroet,
Erik V. - http://www.erikve...

vnpenguin

12/31/2005 11:22:00 PM

0

Thank you for your solution.

Dale Farnsworth has also same solution (emailed to me).

Thank you again!

vnpenguin

12/31/2005 11:28:00 PM

0

Thank you but I'm not yet familar with class/method notions :)
So I'll return to your solution later, when I learned a little more
than actual.

Regards,

Ilmari Heikkinen

1/1/2006 4:22:00 AM

0

On 12/31/05, vnpenguin@gmail.com <vnpenguin@gmail.com> wrote:> Hi,> I'm trying to write a small ruby script which accepts:> 1. Input : File or $stdin> 2. Output : File or $stdoutin_file, out_file = ARGVinput = in_file ? File.open(in_file) : $stdinoutput = out_file ? File.open(out_file,'w') : $stdoutinput.each_line{|line| output.print do_some_thing(line)}input.closeoutput.close_writeoutput.close if output.is_a? FileCheers,Ilmari