From: Brandon Weaver Date: 2012-12-11T16:46:10+09:00 Subject: Re: Dynamically create an array --f46d042fddcaf78a0904d08edf43 Content-Type: text/plain; charset=ISO-8859-1 In this type of case I'd strongly suggest looking into making a class for subject, it saves quite a bit of time in the long run versus a hacked together hash. As more grades and subjects are added the Hash will become slower and more confounded to use. I also don't entirely understand the reasoning behind your layout. Subject should not be on the same level with the student/grade pair. It doesn't make sense in that manner. If we put it in bullet points it's a little clearer: *BAD* - Grades - Subject - Student - Grade *GOOD* * * - Grades - Subject - Students Again, the current hierarchy makes no sense. and it would be far easier to manipulate with a clearer structure that cleanly separates different types of data. The basic frame for a class would be: Grades contain many subjects, each with several students that all have one grade. This can easily change of course, but with the one grade system it works. On Mon, Dec 10, 2012 at 4:58 PM, 7stud -- wrote: > grades = [ > { "subject" => "english", > "Jim" => 94, > "Billy" => 58 > }, > > { "subject" => "math", > "Jim" => 90, > "Billy" => 80 > }, > > { "subject" => "science", > "Jim" => 99, > "Billy" => 58 > }, > > ] > > jim = [] > billy = [] > > grades.each do |hash| > jim << hash['Jim'] > billy << hash['Billy'] > end > > p jim > p billy > > --output:-- > [94, 90, 99] > [58, 80, 58] > > -- > Posted via http://www.ruby-forum.com/. > > --f46d042fddcaf78a0904d08edf43 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable In this type of case I'd strongly suggest looking into making a class f= or subject, it saves quite a bit of time in the long run versus a hacked to= gether hash. As more grades and subjects are added the Hash will become slo= wer and more confounded to use.

I also don't entirely understand the reasoning behind yo= ur layout. Subject should not be on the same level with the student/grade p= air. It doesn't make sense in that manner. If we put it in bullet point= s it's a little clearer:

BAD

  • Grades
  • =
    • Subject
    • Student
      • Grade
<= br>
GOOD

  • Grades
    • Subject
      • Students

Again, the current hierarchy makes no sense. and it would be far eas= ier to manipulate with a clearer structure that cleanly=A0separates=A0diffe= rent types of data.

The basic frame for a class would be: Grades contain ma= ny subjects, each with several students that all have one grade. This can e= asily change of course, but with the one grade system it works.

On Mon, Dec 10, 2012 at 4:58 PM, 7stud -- <l= ists@ruby-forum.com> wrote:
grades =3D [
=A0 { "subject" =3D> "english",
=A0 =A0 "Jim" =3D> 94,
=A0 =A0 "Billy" =3D> 58
=A0 },

=A0 { "subject" =3D> "math",
=A0 =A0 "Jim" =3D> 90,
=A0 =A0 "Billy" =3D> 80
=A0 },

=A0 { =A0"subject" =3D> "science",
=A0 =A0 =A0"Jim" =3D> 99,
=A0 =A0 =A0"Billy" =3D> 58
=A0 },

]

jim =3D []
billy =3D []

grades.each do |hash|
=A0 jim << hash['Jim']
=A0 billy << hash['Billy']
end

p jim
p billy

--output:--
[94, 90, 99]
[58, 80, 58]

--f46d042fddcaf78a0904d08edf43--