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

F

Frank Church

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
 
E

Ezra Zygmuntowicz

[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 Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
P

Phrogz

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)
 
R

Robert Klemme

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)

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
 
F

Frank Church

Frank said:
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
 
S

Stefan Rusterholz

Frank said:
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
 
R

Robert Klemme

Frank said:
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 ;-)

There is an even simpler solution... :)

Regards

robert
 
P

Phrogz

File.new( configfile, File::CREAT|File::WRONLY, 0700){ |f|
f.puts [ header_commands, mysql_commands, dir_commands,
scp_commands ].flatten
}

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

*slaps hand to forehead*

I got all caught up in thinking that Ruby allowed multiple splats per
function/array
e.g. puts [ *a1, *a2, *a3, *a4 ]
and didn't step back to think about what was possible.

I'm happy to see that 1.9 will support multiple splats as rvalues and
in method calls. I hope/assume this also applies to splatting in an
array literal. (Not that what I was trying is as good as the
simplicity Robert has provides.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top