Forum: Ruby my stupid code ...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
38944d5653a9e373103aaa7ff8562d8a?d=identicon&s=25 unknown (Guest)
on 2005-12-31 22:30
(Received via mailing list)
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 !
4b38034fdb66886a91d55fcd12964cee?d=identicon&s=25 Erik Veenstra (Guest)
on 2005-12-31 23:00
(Received via mailing list)
> 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
3a83969376c805ef5b6042191fdb0ff3?d=identicon&s=25 Andreas S. (andreas)
on 2005-12-31 23:05
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
3a83969376c805ef5b6042191fdb0ff3?d=identicon&s=25 Andreas S. (andreas)
on 2005-12-31 23:09
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.
4b38034fdb66886a91d55fcd12964cee?d=identicon&s=25 Erik Veenstra (Guest)
on 2005-12-31 23:30
(Received via mailing list)
>  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/
4b38034fdb66886a91d55fcd12964cee?d=identicon&s=25 Erik Veenstra (Guest)
on 2005-12-31 23:39
(Received via mailing list)
> 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/
38944d5653a9e373103aaa7ff8562d8a?d=identicon&s=25 unknown (Guest)
on 2006-01-01 00:24
(Received via mailing list)
Thank you for your solution.

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

Thank you again!
38944d5653a9e373103aaa7ff8562d8a?d=identicon&s=25 unknown (Guest)
on 2006-01-01 00:33
(Received via mailing list)
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,
E20e89d58211a3631842daecc1245de2?d=identicon&s=25 Ilmari Heikkinen (Guest)
on 2006-01-01 05:24
(Received via mailing list)
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

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
This topic is locked and can not be replied to.