views:

64

answers:

3

Howdy!

I'm sorry if this question is a dumb one, but I must ask. In PHP, we can create a array without declaring it first, althought it isn't considered good pratice. Exercising my newly-knowledge of Ruby, I was writing a code to list the files inside a directory and sort them by their extensions. To do this, I started a loop to put them in differents arrays based on their extensions. Like this:

files_by_ext = {} #edited - my bad, it was supposed to be {}
files_by_ext['css'] = ['file.css','file2.css','file3.css']
files_by_ext['html'] = ['file.html','file2.html','file3.html']

Then I would sort using the keys 'css' and 'html'. But in the process to create the array of "X" files, I needed to verify if the key "X" existed. I couldn't simply push the file(eg. 'file.X').

There is a way to create methods to alter this behavior, so that I can create a array pushing a item without declaring it first?

files.each do |f|
 extension = /\.(.+)$/.match(f)[1].to_s
 files_by_ext[extension] << f
end

And not(that's what I'm doing):

files.each do |f|
 extension = /\.(.+)$/.match(f)[1].to_s
 if !files_by_ext.key?(extension)
  files_by_ext[extension] = [f]
 else
  files_by_ext[extension] << f
 end
end

I'm sorry, I think I wrote too much. :P Thank you for reading.

A: 

I'm not positive that I understand you correctly, but it sounds a bit like you want to default the initial value of an entry to be an empty array. For example, it sounds like a clean solution would be to create a hash in which all it's entries default to an empty array, instead of to nil. In Ruby, you do this as follows:

>> h = Hash.new { |hash, key| hash[key] = Array.new }
{}
>> h[:foo] << "stuff"

This would eliminate your need to check if an entry exists before appending to it.

Pete
This won't work; the default array is always modified if you do it this way.
wuputah
wow, good call. Hadn't realized that since this isn't something i tend to use. updated the post so it's at least correct
Pete
Thank you for your time and answer, you understood my question correctly. 8D
Bored Elf
+1  A: 

In order to set a default value of Array.new, you must pass Hash.new a block and assign a new array into the Hash each time a new key is used. This is the only correct way to do this:

files_by_ext = Hash.new { |hsh, key| hsh[key] = Array.new }

You can then use the keys in that hash as if every key already has an array in it.

files_by_ext['.com'] << 'command.com'

An alternative approach which is very commonly used is to do the following:

files_by_ext = Hash.new
# ... later ...
files_by_ext['.com'] ||= Array.new
files_by_ext['.com'] << 'command.com'
wuputah
I understand. The difficulty I'm having is to adapt my method of thinking. Differents resources, after all. Anyway, thank you for your answer, helped a lot.I didn't justify in my question, but I used strings as key because I couldn't think of a way to get the extension and transform it in a Symbol. Is it possible?
Bored Elf
Yes, you can use `'string'.to_sym`, but I would just use a string here. It makes more sense, I think, since they are file extensions, which are inherently strings. Strings work just fine as hash keys - in fact, you can use _any object_ as a hash key.
wuputah
A: 

So, couple of things:

Unlike in PHP, Ruby makes difference between arrays (Array class) and associative arrays (Hash class). You need key->value type of structure, you should use Hash, which you will access by file extension. Values of your hash can be arrays of filenames.

However, you don't want filenames duplicated within these values, so you should consider using Set class instead of Array. That way you don't need to worry if you already have the same filename already inserted.

Others already wrote about setting the default value of a Hash.

Mladen Jablanović
I'll look into that, thank you for the tip!
Bored Elf