Moving files into alphabetical directories

Hi. Let’s say I have an array like this:

irb(main):008:0> letters
=> [“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”,
“N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”]

and another array like this:

irb(main):001:0> names = %w(simon sadie tyler kirsty kevin keith)
=> [“simon”, “sadie”, “tyler”, “kirsty”, “kevin”, “keith”]

I’d like to end up with either an array or a hash that contains all
letters of the alphabet (letters) with the matching files (names) and
maybe nil for the letters with no value. I hope this makes sense.

As per the subject, this is initially wanted for the purposes of moving
files into directories. However, as FileUtils.mv happily takes an array
(or a hash?), it’d be nice to know how to do this generally.

Thanks for any assistance.

On Wed, Oct 19, 2011 at 9:53 PM, simon harrison
[email protected] wrote:

I’d like to end up with either an array or a hash that contains all
letters of the alphabet (letters) with the matching files (names) and
maybe nil for the letters with no value. I hope this makes sense.

By matching file names you mean the ones starting with that letter?
A way to get a hash that contains a key for each letter with an array
of words starting with that letter:

h = {}
names.each do |name|
letter = name[0,1].upcase #seems the letters must be upcased
(h[letter] ||= []) << name
end

For the letters that don’t get any word, it depends what you want to
do with that. In this solution you will get nil if you try to access
it.

As per the subject, this is initially wanted for the purposes of moving
files into directories. However, as FileUtils.mv happily takes an array
(or a hash?), it’d be nice to know how to do this generally.

Then you can iterate the hash and call FileUtils.mv for each directory
like this:

h.each {|directory, files| FileUtils.mv files, directory}

Hope this helps,

Jesus.

Very helpful as always, Jesus. Thanks.

On Oct 19, 4:47pm, Jess Gabriel y Galn [email protected]
wrote:

A way to get a hash that contains a key for each letter with an array
of words starting with that letter:

h = {}
names.each do |name|
letter = name[0,1].upcase #seems the letters must be upcased
(h[letter] ||= []) << name
end

This is a good place to use Enumerable#group_by.

>> names = %w(simon sadie tyler kirsty kevin keith)
=> ["simon", "sadie", "tyler", "kirsty", "kevin", "keith"]
>> names.group_by { |n| n[0,1].upcase }
=> {"K"=>["kirsty", "kevin", "keith"], "S"=>["simon", "sadie"],

“T”=>[“tyler”]}

On Oct 19, 2011, at 6:43 PM, Yossef M. wrote:

This is a good place to use Enumerable#group_by.

Has #group_by been integrated into Ruby?

Last I heard, it was still only available as part
of ActiveSupport although there was talk of working
it into 1.9.

Dan N.

On Thu, Oct 20, 2011 at 12:43 AM, Yossef M. [email protected]
wrote:

This is a good place to use Enumerable#group_by.

names = %w(simon sadie tyler kirsty kevin keith)
=> [“simon”, “sadie”, “tyler”, “kirsty”, “kevin”, “keith”]

names.group_by { |n| n[0,1].upcase }
=> {“K”=>[“kirsty”, “kevin”, “keith”], “S”=>[“simon”, “sadie”],
“T”=>[“tyler”]}

Quite true !
Thanks for the reminder.

Jesus.

On Thu, Oct 20, 2011 at 1:27 PM, Dan N. [email protected] wrote:

Has #group_by been integrated into Ruby?

Last I heard, it was still only available as part
of ActiveSupport although there was talk of working
it into 1.9.

It is part of core. Fairly easy to check:

http://ruby-doc.org/core-1.8.7/Enumerable.html#method-i-group_by
http://ruby-doc.org/core-1.9.2/Enumerable.html#method-i-group_by

$ irb

RUBY_VERSION
=> “1.9.2”

Enumerable.instance_methods.grep(/group/)
=> [:group_by]

$ irb

RUBY_VERSION
=> [“group_by”]

Plus ri, if you have it working.

On Thu, Oct 20, 2011 at 1:39 PM, Adam P. [email protected]
wrote:

$ irb

RUBY_VERSION
=> [“group_by”]

Whoops. Overzealous editing.

Enumerable.instance_methods.grep(/group/)
=> [“group_by”]

On Oct 20, 2011, at 8:39 AM, Adam P. wrote:

On Thu, Oct 20, 2011 at 1:27 PM, Dan N. [email protected] wrote:

Has #group_by been integrated into Ruby?

It is part of core. Fairly easy to check:

Doh!
When I went looking for #group_by in irb, I erroneously used
Array.methods rather than Array.instance_methods

Sorry for the noise.

Dan

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