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

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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

On 6/28/07, Frank C. [email protected] 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

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

kind regards -botp

On Jun 27, 7:33 pm, Frank C. [email protected] 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)

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

:slight_smile:

Kind regards

robert

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

Regards
Stefan

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs