On 02/06/2011 10:28 PM, Jacob Carlborg wrote:
On 2011-02-06 20:51, Walter Bright wrote:
Jacob Carlborg wrote:
On 2011-02-04 08:34, Jonathan M Davis wrote:
Slices: just one more reason why D's arrays kick the pants of other
languages'
arrays...
- Jonathan M Davis
Ruby has array slices as well. A slice of an array refers to the
original data just like in D. But on the other hand a new instance is
created when making a slice (I assume, since everything is an object
in Ruby).
Can you use an array slice in ruby as an argument to any function that
takes a string?
Since Ruby is a dynamically typed language you can pass in anything you want
and relay on duck typing. The object passed in just have to support the slicing
syntax/method.
Hm, I just tried this and apparently a slice does NOT behave as in D. Modifying
the slice does NOT modify the original array/string, I was completely sure it
did.
I was wondering about this question by Walter. And actually, another answer
would have surprised me, unless some magic would be in play, somewhat
contradictory to Ruby's all-object motto.
I mean, modifying a slice (all or part of it) is replacing 1 or more
element(s), each having their own address/identity, as /objects/ by themselves,
not as variable values. Unlike in static languages (for non-referenced elements).
To get a similar behaviour, you'd need a double referencing/indirect, provided
for instance by putting composite objects (or other collections, provided
they're referenced) in an array, then changing slots of said objects (or
elements of sub-collection). Then (if my reasoning is correct), modifying a
slice would be seen from the original array.
Denis
PS: just tried in python [id() actualy returns address]:
l = [1,2,3]
s = l[1:2]
print l,s
print id(l[1]), id(s[0])
s[0] = 0
print l,s
print id(l[1]), id(s[0])
print
l = [1,[2],3]
s = l[1:2]
print l,s
print id(l[1]), id(s[0])
s[0][0] = 0
print l,s
print id(l[1]), id(s[0])
==>
spir@d:~/prog/python$ python "__essai__.py"
[1, 2, 3] [2]
169172356 169172356
[1, 2, 3] [0]
169172356 169172380
[1, [2], 3] [[2]]
3078505324 3078505324
[1, [0], 3] [[0]]
3078505324 3078505324
--
_________________
vita es estrany
spir.wikidot.com