From: David A. Black on
Just pasted a small correction to that last pastie.

AND... if you don't care what order the people are in, you could
actually do this:

def self.run
spies = get_people_from_file.sort_by { rand }
spies.each_with_index do |spy,i|
puts "Person #{spy} spies on Person #{spies[i-1]}"
end
end

That randomizes the array up front, and then just pairs people as
spy/spyee. Given that approach, you don't need to test for spyability.


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com

From: Rick DeNatale on
On Sun, Jul 18, 2010 at 5:03 AM, James Rasmussen <jamesrasmus(a)gmail.com> wrote:
> However, dup copies instance variables over, and it IS a shallow copy,
> but if you change an instance variable, based on my debugging, it
> changes the original as well.

James, at this point in your journeys in Ruby, it might be well to
understand the difference between variables and objects.

The dup method doesn't act on variables, like all other methods in
Ruby it acts on an object. Variables are simply references to
objects.

http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objects

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

From: Matthew Pounsett on

On 2010/07/17, at 06:38, James Rasmussen wrote:

> I'm having a problem with my code. It's attached, and if someone could
> look at it, that would be great. I can't figure out what I'm doing
> wrong. I think it's something wrong with my for loop. Basically, the
> code is supposed to find "spies" for all the entries, but someone cannot
> "spy" on a person with their same last name, or themselves. The goal is
> to print the list of spies all out. Thank you in advance!

This won't help with your logic problem as it stands .. but it looks like others have commented on that.

As another approach, Array.permutation (ruby 1.9) might be of some use in solving your problem. The method will give you all permutations of the elements of an array (which doesn't include matching an element with itself) ... if you've got an array of arrays, then all you need to do is reject permutations where the last-name position in both arrays contain the same name... same deal if it's an array of hashes.

I started to insert some sample code here, but then realized that would probably defeat the purpose of your exercise. :)

If you're not using 1.9, then I'm sure you can find a quick way to implement the equivalent.

HTH,
Matt



From: James Rasmussen on
Hi,

Thanks everyone who's helped out with this. I finally got my original
code working, simply by making a rand_array at the same time I was
making my original array, thus duplicating the array without duplicating
the references to the objects.

It seems fascinating to me that (I've looked this up on other people's
blogs for reference)
http://thingsaaronmade.com/blog/ruby-shallow-copy-surprise.html that
duplicating an array of objects using array.dup STILL copies the
references to the objects over. There's no such as a "C++" shallow copy
in Ruby, because there are no primitive datatypes, and everything is an
object. That's why when I changed the boolean value "found" that is a
member variable in my persons class, that is found in my original spies
array, it changed it in rand_spies as well.

In short, I'm slowly learning how to use ruby to solve problems, and
learning how to not work against it. It really has some beautiful
features and syntax that make me smile whilst sitting at my computer.
"wow, did I just do that in one line?" I just need to get used to the
nature of the animal. It's different than other languages I've worked in
(Java, C++, PHP), and it's actually, in the short time I've been
learning it, made me a better programmer in these other languages,
because I approach everything in a much more elegant, OO way.

Thanks again,

James Rasmussen
--
Posted via http://www.ruby-forum.com/.