# Is there a nifty ruby hack for this set of commands?

**URL:** <https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986>\
**Category:** Ruby\
**Created:** [June 28, 2007, 3:33am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986 "2007-06-28T03:33:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 28, 2007, 3:33am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/1 "2007-06-28T03:33:46Z")

</div>

I have this sequence of commands to write a list of values to a file

f = File.new(configfile, File::CREAT|File::WRONLY, 0700)  
header\_commands.each {|x| f.puts x }  
mysql\_commands.each {|x| f.puts x }  
dir\_commands.each {|x| f.puts x }  
scp\_commands.each {|x| f.puts x }

Is there a way to do something like

[header\_commands, mysql\_commands, dir\_commands, scp\_commands] each {|x|  
f.puts x }

I know there must be a number of ruby ways to do it.

Any examples?

- Frank

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 28, 2007, 3:43am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/2 "2007-06-28T03:43:18Z")

</div>

On Jun 27, 2007, at 6:33 PM, Frank C. wrote:

> [header\_commands, mysql\_commands, dir\_commands, scp\_commands] each  
> {|x|  
> f.puts x }

[header\_commands, mysql\_commands, dir\_commands,  
scp\_commands].flatten.each {|x| f.puts x }

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 28, 2007, 3:47am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/3 "2007-06-28T03:47:19Z")

</div>

On 6/28/07, Frank C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to do something like
> 
> [header\_commands, mysql\_commands, dir\_commands, scp\_commands] each {|x|  
> f.puts x }
> 
> I know there must be a number of ruby ways to do it.
> 
> Any examples?
> 
> - Frank

Just a stab. Besides a normal nested loop, you could add all the  
array’s of  
commands together and them join them

puts ( header\_commands + mysql\_commands + dir\_commands + scp\_commands  
).join(“\n”)

or a little less hard coded

puts ( commands.inject( [] ){ |list, current| list + current  
}.join(“\n”)

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 28, 2007, 4:34am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/4 "2007-06-28T04:34:22Z")

</div>

On Behalf Of Frank C.:

# f = File.new(configfile, File::CREAT|File::WRONLY, 0700)

i’d prefer to you use the block mode

# header\_commands.each {|x| f.puts x }

# mysql\_commands.each {|x| f.puts x }

# dir\_commands.each {|x| f.puts x }

# scp\_commands.each {|x| f.puts x }

# 

# Is there a way to do something like

# 

# [header\_commands, mysql\_commands, dir\_commands, scp\_commands]

# each {|x|

# f.puts x }

assumming x\_commands are arrays, read on array#:+ or array#flatten.

but you can still stick to your original algo and since it’s just an  
array of arrays, you just nest like,

m\_list = [] # ----------------------------------------  
m\_list \<\< header\_commands # the adv here is that i can put comments  
m\_list \<\< mysql\_commands # and can rearrange/preprocess commands  
m\_list \<\< dir\_commands # without touching the main/meat loop code  
m\_list \<\< scp\_commands # ----------------------------------------  
m\_list.each do |cmdlist|  
cmdlist.each do |x|  
f.puts x  
end  
end

if you want to be fancy, try yaml so you can save/restore the command  
groups anytime.

even rio will tickle your fancy,

rio(“a.txt”) \<\< m\_list.join("\n")

# I know there must be a number of ruby ways to do it.

you bet. watch this thread 🙂

kind regards -botp

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 28, 2007, 7:40am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/5 "2007-06-28T07:40:33Z")

</div>

On Jun 27, 7:33 pm, Frank C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> f = File.new(configfile, File::CREAT|File::WRONLY, 0700)  
> header\_commands.each {|x| f.puts x }  
> mysql\_commands.each {|x| f.puts x }  
> dir\_commands.each {|x| f.puts x }  
> scp\_commands.each {|x| f.puts x }

File.new( configfile, File::CREAT|File::WRONLY, 0700){ |f|  
f.puts [ header\_commands, mysql\_commands, dir\_commands,  
scp\_commands ].flatten  
}

(Since puts will already join the array with \n)

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 28, 2007, 11:50am UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/6 "2007-06-28T11:50:57Z")

</div>

On 28.06.2007 07:35, Phrogz wrote:

> }
> 
> (Since puts will already join the array with \n)

I believe there is an even simpler solution:

File.open configfile, File::CREAT|File::WRONLY, 0700 do |f|  
f.puts header\_commands,  
mysql\_commands,  
dir\_commands,  
scp\_commands  
end

🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 30, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/7 "2007-06-30T13:38:11Z")

</div>

Frank C. wrote:

> I have this sequence of commands to write a list of values to a file
> 
> f = File.new(configfile, File::CREAT|File::WRONLY, 0700)  
> header\_commands.each {|x| f.puts x }  
> mysql\_commands.each {|x| f.puts x }  
> dir\_commands.each {|x| f.puts x }  
> scp\_commands.each {|x| f.puts x }
> 
> Is there a way to do something like
> 
> [header\_commands, mysql\_commands, dir\_commands, scp\_commands] each {|x|  
> f.puts x }
> 
> I know there must be a number of ruby ways to do it.
> 
> Any examples?

You’re almost there  
[header\_commands, mysql\_commands, dir\_commands, scp\_commands].each {|x|  
f.puts \*x  
}

You only missed a . and a \*, that’s it 😉

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 30, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/is-there-a-nifty-ruby-hack-for-this-set-of-commands/107986/8 "2007-06-30T12:33:49Z")

</div>

Frank C. wrote:

> I have this sequence of commands to write a list of values to a file
> 
> f = File.new(configfile, File::CREAT|File::WRONLY, 0700)  
> header\_commands.each {|x| f.puts x }  
> mysql\_commands.each {|x| f.puts x }  
> dir\_commands.each {|x| f.puts x }  
> scp\_commands.each {|x| f.puts x }
> 
> Is there a way to do something like
> 
> [header\_commands, mysql\_commands, dir\_commands, scp\_commands] each {|x|  
> f.puts x }
> 
> I know there must be a number of ruby ways to do it.
> 
> Any examples?
> 
> - Frank

Thanks for all the examples.

With this kind of support, I think ruby is a great choice.

- Frank
