[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Frank Church

6/28/2007 1:34:00 AM


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

7 Answers

Ezra Zygmuntowicz

6/28/2007 1:43:00 AM

0


On Jun 27, 2007, at 6:33 PM, Frank Church 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 Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



Gavin Kistner

6/28/2007 5:36:00 AM

0

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)

Robert Klemme

6/28/2007 9:46:00 AM

0

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)

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

Frank Church

6/30/2007 10:34:00 AM

0

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

Stefan Rusterholz

6/30/2007 11:39:00 AM

0

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

Regards
Stefan

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

Robert Klemme

7/1/2007 5:38:00 PM

0

On 30.06.2007 13:38, Stefan Rusterholz wrote:
> 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 ;-)

There is an even simpler solution... :-)

Regards

robert

Gavin Kistner

7/2/2007 4:50:00 AM

0

On Jun 28, 3:46 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 28.06.2007 07:35, Phrogz wrote:
> > 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.)

--
(- /\ \/ / /\/