[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:02088] Re: Array trouble

From: Dave Thomas <Dave@...>
Date: 2000-03-22 23:07:39 UTC
List: ruby-talk #2088
"Dat Nguyen" <thucdat@hotmail.com> writes:

> $ ruby -v
> ruby 1.4.3 (1999-12-08) [i586-linux]
> $ruby sample/irb.rb
> irb(main):001:0> ary = Array.new(3,5)
> [5, 5, 5]
> irb(main):002:0> ary[0..2] = 7
> 7
> irb(main):003:0> ary
> [7]
> irb(main):004:0> ary[0]
> 7
> irb(main):005:0> ary[1]
> nil
> irb(main):006:0>
> 
> According to the Ruby manual:
> self[start..end] = val
> Replace the items from start to end with val. If val is not an array,
> the type will be converted into the Array using to_a method.
> 
> But my exercise above did not do that. Why?

Actually it _did_ do it, but it just didn't do what you were
expecting. a[0..2] = <something> replaces the slice of array a[0],
a[1], and a[2] with something. Take a slightly different case

   a = (1..10).to_a  
   a -> 1 2 3 4 5 6 7 8 9 10

   a[3..6] = 'cat'
   a -> 1 2 3 'cat' 8 9 10

You see, the slice of the array from a[3] to a[6] was replaced with
'cat'. As the replacement was shorter than the original, all
subsequent entries were moved down.


Regards


Dave

In This Thread

Prev Next