Re: [Wtr-general] OT: The original problem. Re: A small doubt in Ruby

2007-03-05 Thread Charley Baker

Strange, I tried the example from Jonathan Kohl, and it worked fine.

The idea in Ruby is that variables hold references to objects not the
objects themselves similar to Java. If you've worked in c/c++ this will make
more sense, Ruby, Java and some other languages hide the ugliness of this
under the covers. So when you create a variable and assign it to something,
it's holding a pointer to the actual object. When you then assign that to
another variable, you've now got two variables pointed to the same object in
memory.

a = 'foo' # create a variable named a that points to an object somewhere
in memory
b = a # now create b which will point to that same object in memory,
in this case a string object
puts a.object_id
=> object_id whatever
puts b.object_id
=> the same object_id since they both point to the same object in memory

Enumerating through a collection of some sort like an Array, makes it a bit
more confusing. If you end up returning strings, for example, then those are
new objects which won't then hold reference to the original objects in your
collection. Hopefully that makes some sense. :)

-Charley


On 3/5/07, Chris McMahon <[EMAIL PROTECTED]> wrote:


On 3/5/07, vijay <[EMAIL PROTECTED]> wrote:
> Thank you Zeljko Filipin.  That was an useful and an interesting '.pdf'.
>
> Thanks,
> Vijay.

For those interested in both history and the vagaries of OO
programming, the original issue was a problem I stumbled across and
mentioned on the list for people reviewing Brian's book.  It's an
interesting issue, and a good thing to be aware of, if you aren't
already.  For me, it comes up when I work with databases, since what
comes back from Ruby ODBC is almost always an AoA, and that's where I
got into trouble...

#
require 'test/unit'
class TOY_CASE expected but was
<[1, 2, 3]>.

1 tests, 1 assertions, 1 failures, 0 errors
>Exit code: 1

I had to go to the comp.lang.ruby list for an answer.  Even my local
Ruby guru didn't see the problem immediately.

Apparently inside an "each" loop you're only working on a copy of the
item, not the item itself, so to_s isn't actually working on the real
items in the array.

To make this work, you have to use "map":

aoa.each do |arr|
   arr.map! do |item|
   item = item.to_s
   end


Jonathan Kohl mentions an opposite but similar weirdness:

a = %w[a b c]
b = a
b = %w[x y z]
p a
=>
x y z
p b
=>
x y z
when what was expected was this:
p a
=>
["a", "b", "c"]
___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general

___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general

Re: [Wtr-general] OT: The original problem. Re: A small doubt in Ruby

2007-03-06 Thread Bret Pettichord
> Apparently inside an "each" loop you're only working
> on a copy of the
> item, not the item itself, so to_s isn't actually
> working on the real
> items in the array.

This is not true. The problem is not that you have a copy, but rather that your 
reference ('item") is local to the block. Changing it doesn't change anything 
outside the block. The problem isn't with what you are given in the block, but 
rather with what you are doing with it.

> aoa = [[1,2,3],[4,5,6]]

> aoa.each do |arr|
> arr.each do |item|
> item = item.to_s
> end 

> To make this work, you have to use "map":
> 
> aoa.each do |arr|
>arr.map! do |item|
> item = item.to_s
>end

Note that "item" is a local variable and that therefore changing its value has 
absolutely no effect. In other words, this code does exactly the same thing:

 aoa.each do |arr|
arr.map! do |item|
   item.to_s
end

Your post suggested that "map!" increases the scope of the bound variable. But 
actually what it does is replace the item in arr with the value returned by the 
block. A side effect of assignment is that it returns the value, and that is 
what is actually doing the work.

Bret
-
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=6791&messageID=19606#19606
___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general


Re: [Wtr-general] OT: The original problem. Re: A small doubt in Ruby

2007-03-09 Thread Jonathan Kohl
> Jonathan Kohl mentions an opposite but similar weirdness:
> 
>  a = %w[a b c]
> b = a
>  b = %w[x y z]
> p a
>  =>
> x y z
>  p b
> =>
>  x y z
> when what was expected was this:
>  p a
> =>
> ["a", "b", "c"]
> ___

I wouldn't characterize it as weirdness, it is just a pitfall that is common 
when people assign values in one way, and expect collections to behave the same 
way:
irb(main):001:0> a =
=> 1
irb(main):002:0> b =
=> 1
irb(main):003:0> p a
1
=> nil
irb(main):004:0> p b
1
=> nil
irb(main):005:0> b =
=> 2
irb(main):006:0> p a
1
=> nil
irb(main):007:0> p b
2
=> nil

As Marick says, variables and objects are different kinds of things. The name 
of the thing is not the thing itself. Variables do not contain objects, they 
point to them. In the array example Chris posted, we assign "a", a reference 
which "points" to the collection containing "["a", "b","c"]", and then we 
assign "b", which is a reference to "a", not the collection. That can trip 
people up if they are used to doing things like I posted above.
(I think I have that right, Bret or someone else could explain it better.)

 In Head First Java, Kathy Sierra explains this really well using pictures of 
remote controls pointing to objects. Marick had a really nice set of pictures 
for Scripting for Testers that explains this, but I'm not sure if it's in the 
book or not. 

-Jonathan
-
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=6791&messageID=19731#19731
___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general


Re: [Wtr-general] OT: The original problem. Re: A small doubt in Ruby

2007-03-10 Thread Bret Pettichord
> As Marick says, variables and objects are different
> kinds of things. The name of the thing is not the
> thing itself. Variables do not contain objects, they
> point to them. In the array example Chris posted, we
> assign "a", a reference which "points" to the
> collection containing "["a", "b","c"]", and then we
> assign "b", which is a reference to "a", not the
> collection. That can trip people up if they are used
> to doing things like I posted above.
> (I think I have that right, Bret or someone else
> could explain it better.)

Actually B is not a reference to A, but rather a reference to the same 
collection that A points to. They are both equally valid names for the same 
thing.
-
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=6791&messageID=19748#19748
___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general


Re: [Wtr-general] OT: The original problem. Re: A small doubt in Ruby

2007-03-12 Thread Jonathan Kohl
> > (I think I have that right, Bret or someone else
> > could explain it better.)
> Actually B is not a reference to A, but rather a
> reference to the same collection that A points to.
> They are both equally valid names for the same thing.
And on cue.. :-)

Thanks for clearing that up.

-Jonathan
-
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=6791&messageID=19763#19763
___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general