I have an array that contains 36 objects. The composition of those
objects includes a date, a team, and a time. It is being parsed from
another site.
[0] = Sept 2
[1] = vsSouthern Miss
[2] = 7:30 PM ET
[3] = Sept 9
[4] = @Florida
[5] = 7:30 PM ET
etc.
Every 3 objects should be combined into a specific hash row.
[0][:date] = Sept 2
[0][:team] = vsSouthern Miss
[0][:time] = 7:30 PM ET
[1][:date] = Sept 9
[1][:team] = @Florida
[1][:time] = 7:30 PM ET
etc.
I'm having trouble accomplishing this. Each schedule might have more
than 36 objects in the array, but the divisor will always be 3 in terms
of what type of information is presented. So, a team might have an
array of 39 objects or 42 objects but in the end, the objects are a
date, team, and a time.
I've tried numerous things, even adding a new class method:
class Array
def to_spec_hash(other)
Hash[ *(0...self.size()).inject([]) { |arr, ix| arr.push(self[ix],
other[ix]) } ]
end
end
and then trying to use:
%W{ date team time }.to_spec_hash( %W{ value1 value2 value3 etc. } )
but I'd have to iterate over date team and time according to how many
rows of data divided by 3 would be and the same for the values. Again,
I know it's sloppy.
I'm sure there's a much faster and a cleaner way of accomplishing this
but I'm lacking sleep and therefore not thinking as clearly.
Any help would be appreciated.
···
--
Posted via http://www.ruby-forum.com/.
Is this what you want?
input = [
"Sept 2",
"vsSouthern Miss",
"7:30 PM ET" ,
"Sept 9" ,
"@Florida" ,
"7:30 PM ET"
]
input.extend Enumerable
p input.enum_for(:each_slice, 3).map { |date, team, time|
{:date => date, :team => team, :time => time}
}
[{:date=>"Sept 2", :team=>"vsSouthern Miss", :time=>"7:30 PM ET"},
{:date=>"Sept 9", :team=>"@Florida", :time=>"7:30 PM ET"}]
This is written for 1.8.6, for 1.8.7 or 1.9.x Enumerable is built in
in 1.9 and you can get the enumerator by calling each_slice without a
block.
p (input.each_slice(3)).map { |date, team, time|
{:date => date, :team => team, :time => time}
}
In any event if it were me I'd make a Game class, or at least use an
OpenStruct instead of a Hash
···
On Sat, Aug 14, 2010 at 10:17 AM, Alpha Blue <jdezenzio@gmail.com> wrote:
I have an array that contains 36 objects. The composition of those
objects includes a date, a team, and a time. It is being parsed from
another site.
[0] = Sept 2
[1] = vsSouthern Miss
[2] = 7:30 PM ET
[3] = Sept 9
[4] = @Florida
[5] = 7:30 PM ET
etc.
Every 3 objects should be combined into a specific hash row.
[0][:date] = Sept 2
[0][:team] = vsSouthern Miss
[0][:time] = 7:30 PM ET
[1][:date] = Sept 9
[1][:team] = @Florida
[1][:time] = 7:30 PM ET
etc.
I'm having trouble accomplishing this. Each schedule might have more
than 36 objects in the array, but the divisor will always be 3 in terms
of what type of information is presented. So, a team might have an
array of 39 objects or 42 objects but in the end, the objects are a
date, team, and a time.
I've tried numerous things, even adding a new class method:
class Array
def to_spec_hash(other)
Hash[ *(0...self.size()).inject() { |arr, ix| arr.push(self[ix],
other[ix]) } ]
end
end
and then trying to use:
%W{ date team time }.to_spec_hash( %W{ value1 value2 value3 etc. } )
but I'd have to iterate over date team and time according to how many
rows of data divided by 3 would be and the same for the values. Again,
I know it's sloppy.
I'm sure there's a much faster and a cleaner way of accomplishing this
but I'm lacking sleep and therefore not thinking as clearly.
Any help would be appreciated.
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
Thanks Rick - this example did work but I believe your second idea was a
better one. I'll look into that instead.
···
--
Posted via http://www.ruby-forum.com/.