# My stupid code

**URL:** <https://www.ruby-forum.com/t/my-stupid-code/85353>\
**Category:** Ruby\
**Created:** [December 31, 2005, 10:30pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353 "2005-12-31T22:30:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 10:30pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/1 "2005-12-31T22:30:09Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 11:00pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/2 "2005-12-31T23:00:14Z")

</div>

> 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/](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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 11:05pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/3 "2005-12-31T23:05:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 11:09pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/4 "2005-12-31T23:09:20Z")

</div>

Erik V. 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 11:30pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/5 "2005-12-31T23:30:17Z")

</div>

> 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/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 31, 2005, 11:39pm UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/6 "2005-12-31T23:39:18Z")

</div>

> 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/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 1, 2006, 12:24am UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/7 "2006-01-01T00:24:21Z")

</div>

Thank you for your solution.

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

Thank you again!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 1, 2006, 12:33am UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/8 "2006-01-01T00:33:23Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 1, 2006, 5:24am UTC](https://www.ruby-forum.com/t/my-stupid-code/85353/9 "2006-01-01T05:24:53Z")

</div>

On 12/31/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
