My stupid code

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 :slight_smile: 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 !

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

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

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-forum.com/\.

in_file, out_file = ARGV
input = in_file ? File.open(in_file) : $stdin
output = out_file ? File.open(out_file,'w') : $stdout

input.each_line{|line|
  output.print do_some_thing(line)
}

input.close
output.close_write
output.close if output.is_a? File

Cheers,
Ilmari

···

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 $stdout

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-forum.com/\.

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

Thank you for your solution.

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

Thank you again!

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

Regards,

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... :slight_smile:

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/