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

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

···

--
Posted via http://www.ruby-forum.com/.

[header_commands, mysql_commands, dir_commands, scp_commands].flatten.each {|x| f.puts x }

Cheers-
-- Ezra Zygmuntowicz-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)

···

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

[header_commands, mysql_commands, dir_commands, scp_commands] each {|x|
f.puts x }

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

···

On 6/28/07, Frank Church <vfcforums@gmail.com> 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

On Behalf Of Frank Church:
# 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 :slight_smile:

kind regards -botp

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)

···

On Jun 27, 7:33 pm, Frank Church <vfcfor...@gmail.com> 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 }

Frank Church 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

···

--
Posted via http://www.ruby-forum.com/\.

Frank Church 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 :wink:

Regards
Stefan

···

--
Posted via http://www.ruby-forum.com/\.

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

:slight_smile:

Kind regards

  robert

···

On 28.06.2007 07:35, Phrogz wrote:

On Jun 27, 7:33 pm, Frank Church <vfcfor...@gmail.com> 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)