Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to make a variable based on array length and member value

Reply
Thread Tools

how to make a variable based on array length and member value

 
 
Catsquotl
Guest
Posts: n/a
 
      06-28-2009
Hi

I have an array full_array[] of an arbitrary number of transaction objects.
within the transaction is a Date member..

From the array I have created a new array years[].uniq! based on the
Date.year members

what id like to do is take the new arrays members and use them as names
for variables..

Say the array holds members for 2007, 2008, 2009
i`d like to iterate through the full_array and map all members from 2008
in an array.

my options are creating an array based on years.length where each member
holds all transactions from a specific year.

or iterate through the members of years and create an array based on the
value or index of years..

I can`t seem to think of the logic to accomplish this..

Any ideas?

Eelco


 
Reply With Quote
 
 
 
 
Ian Hobson
Guest
Posts: n/a
 
      06-28-2009
Catsquotl wrote:
> Hi
>
> I have an array full_array[] of an arbitrary number of transaction
> objects.
> within the transaction is a Date member..
>
> From the array I have created a new array years[].uniq! based on the
> Date.year members
>
> what id like to do is take the new arrays members and use them as
> names for variables..
>
> Say the array holds members for 2007, 2008, 2009
> i`d like to iterate through the full_array and map all members from
> 2008 in an array.
>
> my options are creating an array based on years.length where each
> member holds all transactions from a specific year.
>
> or iterate through the members of years and create an array based on
> the value or index of years..
>
> I can`t seem to think of the logic to accomplish this..
>
> Any ideas?
>

Build up a suite of tests and the code in parallel, like this.

1) Think of a very simple case that isn't yet covered by your code.
Start with a very simple cases, and proceed to edge cases and
pathological cases.

2) Write a new test to run your code and test the result, and run it. It
will fail, because you HAVE NOT YET WRITTEN THE CODE. (If it doesn't
fail, you have made a mistake somewhere).

3) Alter your code to pass the new test and all of the old ones. Only
when this is the case, may you proceed to stage 4.

4) Improve you code in some small way - Remove duplicate code, repair a
bad choice of variable names etc.

5) After each improvement, Rerun all the tests. If any fail, you
introduced an error. Fix before proceeding.

6) When the all pass and your code is nice and clean, save code (and
tests) to the version control system.

7) If the code is not yet complete, go to step 1.

The key is to go in really tiny baby steps at stages 1 and 4, and to
push stage 4 until you have really nice code you would be proud to show
anyone. Note - you are testing for anything that might break/be broken,
not aiming for 100% code coverage.

You will find that defining the tests will clarify your thinking about
what is required. Stage 4 means the code remains clean and easy to
modify. The test suite will stop you introducing errors into your code
later.

When you are done, you have nice code, that you can demonstrate is correct.

Regards

Ian


 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      06-29-2009
Catsquotl wrote:
> I have an array full_array[] of an arbitrary number of transaction
> objects.
> within the transaction is a Date member..
>
> From the array I have created a new array years[].uniq! based on the
> Date.year members
>
> what id like to do is take the new arrays members and use them as names
> for variables..
>
> Say the array holds members for 2007, 2008, 2009
> i`d like to iterate through the full_array and map all members from 2008
> in an array.


Sounds like what you want is a Hash, not an Array. Something like:

year_to_members = {}
members.each do |mem|
y = mem.date.year
year_to_members[y] ||= []
year_to_members[y] << mem
end

p year_to_members[2009]
# this prints an array containing elememnts where date.year == 2009

As others will probably point out, this pattern can be simplified a bit.
But hopefully it's clear enough as a starting point.

The fundamental point is, you don't want to create "names of variables"
for each year - even though that is technically possible, as others may
also point out. The right thing to do is to build a Hash, which is an
object which associates a key (a year number) with a value (an array of
transaction objects).

HTH,

Brian.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-29-2009
2009/6/29 Brian Candler <>:
> Catsquotl wrote:


>> Say the array holds members for 2007, 2008, 2009
>> i`d like to iterate through the full_array and map all members from 2008
>> in an array.

>
> Sounds like what you want is a Hash, not an Array. Something like:


> As others will probably point out, this pattern can be simplified a bit.


Since you asked...

year_to_members = Hash.new {|h,k| h[k] = []}

members.each do |mem|
year_to_members[mem.date.year] << mem
end

> The fundamental point is, you don't want to create "names of variables"
> for each year - even though that is technically possible, as others may
> also point out. The right thing to do is to build a Hash, which is an
> object which associates a key (a year number) with a value (an array of
> transaction objects).


100% agree. It is definitively a bad idea to create variables with
the year in their name.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      06-29-2009
Hi --

On Sun, 28 Jun 2009, Catsquotl wrote:

> Hi
>
> I have an array full_array[] of an arbitrary number of transaction objects.
> within the transaction is a Date member..
>
> From the array I have created a new array years[].uniq! based on the
> Date.year members
>
> what id like to do is take the new arrays members and use them as names for
> variables..
>
> Say the array holds members for 2007, 2008, 2009
> i`d like to iterate through the full_array and map all members from 2008 in
> an array.
>
> my options are creating an array based on years.length where each member
> holds all transactions from a specific year.
>
> or iterate through the members of years and create an array based on the
> value or index of years..
>
> I can`t seem to think of the logic to accomplish this..


You could use group_by (if you've got Ruby 1.9 or a 1.9-flavored 1.:

full_array.group_by {|transaction| transaction.date.year }

That will give you a hash whose keys are the years and whose values
are arrays of transaction objects, one such array per year-key.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com

 
Reply With Quote
 
Catsquotl
Guest
Posts: n/a
 
      06-29-2009
David A. Black schreef:
>
> You could use group_by (if you've got Ruby 1.9 or a 1.9-flavored 1.:
>
> full_array.group_by {|transaction| transaction.date.year }
>
> That will give you a hash whose keys are the years and whose values
> are arrays of transaction objects, one such array per year-key.
>
>
> David
>

Thanks for the reply..(Robert, Brian also)
As a matter of fact I installed 1.9 last week awaiting the arrival of
the well-grounded rubyist which should be sometime tomorrow

Eelco
 
Reply With Quote
 
Catsquotl
Guest
Posts: n/a
 
      06-29-2009
Ian Hobson schreef:
> Build up a suite of tests and the code in parallel, like this.
>
> 1) Think of a very simple case that isn't yet covered by your code.
> Start with a very simple cases, and proceed to edge cases and
> pathological cases.
>
> 2) Write a new test to run your code and test the result, and run it. It
> will fail, because you HAVE NOT YET WRITTEN THE CODE. (If it doesn't
> fail, you have made a mistake somewhere).
>
> 3) Alter your code to pass the new test and all of the old ones. Only
> when this is the case, may you proceed to stage 4.
>
> 4) Improve you code in some small way - Remove duplicate code, repair a
> bad choice of variable names etc.
>
> 5) After each improvement, Rerun all the tests. If any fail, you
> introduced an error. Fix before proceeding.
>
> 6) When the all pass and your code is nice and clean, save code (and
> tests) to the version control system.
>
> 7) If the code is not yet complete, go to step 1.
>
> The key is to go in really tiny baby steps at stages 1 and 4, and to
> push stage 4 until you have really nice code you would be proud to show
> anyone. Note - you are testing for anything that might break/be broken,
> not aiming for 100% code coverage.
>
> You will find that defining the tests will clarify your thinking about
> what is required. Stage 4 means the code remains clean and easy to
> modify. The test suite will stop you introducing errors into your code
> later.
>
> When you are done, you have nice code, that you can demonstrate is correct.
>
> Regards
>
> Ian

Hi Ian,

As I am a relative beginner do you know any sites/articles/tutorials on
test driven development?
All I could find was sites on the benefits of tdd but not a tutorial
within the ruby language.. found a few in Java

I have manually tested all classes I write until now which is a bit
tedious and time consuming..

I had a quick look at the test/unit Rdocs but am not quite certain on
how to use them

Eelco


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Flexible array member + variable length array Adam Warner C Programming 10 02-10-2005 10:11 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments