my stupid code ...

V

vnpenguin

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 !
 
E

Erik Veenstra

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.erikveen.dds.nl/

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

# 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

----------------------------------------------------------------
 
A

Andreas S.

unknown said:
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
 
A

Andreas S.

Erik said:
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.
 
E

Erik Veenstra

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.erikveen.dds.nl/
 
V

vnpenguin

Thank you for your solution.

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

Thank you again!
 
V

vnpenguin

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,
 
I

Ilmari Heikkinen

T24gMTIvMzEvMDUsIHZucGVuZ3VpbkBnbWFpbC5jb20gPHZucGVuZ3VpbkBnbWFpbC5jb20+IHdy
b3RlOgo+IEhpLAo+IEknbSB0cnlpbmcgdG8gd3JpdGUgYSBzbWFsbCBydWJ5IHNjcmlwdCB3aGlj
aCBhY2NlcHRzOgo+ICAgMS4gSW5wdXQgOiBGaWxlIG9yICRzdGRpbgo+ICAgMi4gT3V0cHV0IDog
RmlsZSBvciAkc3Rkb3V0Cgppbl9maWxlLCBvdXRfZmlsZSA9IEFSR1YKaW5wdXQgPSBpbl9maWxl
ID8gRmlsZS5vcGVuKGluX2ZpbGUpIDogJHN0ZGluCm91dHB1dCA9IG91dF9maWxlID8gRmlsZS5v
cGVuKG91dF9maWxlLCd3JykgOiAkc3Rkb3V0CgppbnB1dC5lYWNoX2xpbmV7fGxpbmV8CiAgb3V0
cHV0LnByaW50IGRvX3NvbWVfdGhpbmcobGluZSkKfQoKaW5wdXQuY2xvc2UKb3V0cHV0LmNsb3Nl
X3dyaXRlCm91dHB1dC5jbG9zZSBpZiBvdXRwdXQuaXNfYT8gRmlsZQoKQ2hlZXJzLApJbG1hcmkK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top