[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6534] Re: Question about the behavior of write attributes in blocks

From: Dave Thomas <Dave@...>
Date: 2000-11-22 22:50:09 UTC
List: ruby-talk #6534
"Christoph Rippel" <chr@subdimension.com> writes:

> Thank you for explanation - the output of "x".inspect() is
> 
> >>"x"
> 
> which made me assume that Strings are implemented as singletons  (which
> doesn't make much sense in the presence of a replace() method).
> 
> I was hoping to be able to write
> ***********
> x =  [1,2,3]
> x.each { |i|    i+= 1  }

You could write this as

  [1,2,3].collect! {|i| i+1}		# => [2, 3, 4]

This replaces each value with the result of the block.

> but there is no such method for Fixnum and the only way I can think about
> writing such a method always involves the use of the ``=()''-operator -
> which means I am effectively stuck. The String Class seems to be one of the
> few build in classes sporting a replace() method so I can write

There's a reason for this. Let's go back to Strings

  a = "cat"     # => "cat"
  b = a		# => "cat"
  a[0] = "b"	
  b		# => "bat"

We changed 'a', but 'b' got the value too. Why? Because both a and b
reference the same string object.

Now, let's look at what would happen if Fixnum had the same behavior:

  ## not real code ##
  a = 1        # => 1
  b = a        # => 1
  b.increment  # => 2
  a            # => 2

So we've just changed the value of '1' throughout the system. This
would be "a bad thing". So, Fixnums have value semantics, not
reference semantics. Variables hold the value, not a reference to the
value. That's why your first example doesn't work:

   [1,2,3].each { |i| i += 1 }

The variable 'i' receives successive Fixnum values, not
references. Changes to the value stored in the variable therefore
don't affect numbers globally.

> This is only a stop-cap measure since I really want to manipulate
> Arrays (or binary trees) of Fixnum or more general any Object.

Your collection can provide a #collect! method to change the values.


Regards


Dave

In This Thread