From: Josselin Date: 2006-10-20T20:50:08+09:00 Subject: Re: filling an array excepted first and last position... On 2006-10-20 10:54:24 +0200, "Tomasz Wegrzanowski" said: > On 10/20/06, Josselin wrote: >> ldom = 30 # variable (last day of a month...) >> >> # array init to 0 >> @obk = [] >> 1.step(ldom, 1) do |i| >> @obk[i] = 0 >> end >> >> I am filling an array as follows (part of a larger filling scheme) : >> ---- >> #fiiling the array upon another array content >> # but I'd like to AVOID filling the first and last position in this loop.... >> obdays.each do |bd| >> @obk[bd] = 1 >> end > > How about: > @obk = Array.new(ldom, 0) > obdays[1..-2].to_a.each{|bd| @obk[bd] = 1} interesting but when I enter this command obdays is already a range : > obdays = > 2..6 so I want to do someting like that : obdays[3..5].to_a.each{|bd| @obk[bd] = 1} starting the position after the first in the range and ending with the position before the last like obdays[start+1..end-1].to_a.each{|bd| @obk[bd] = 1} ? > > to_a just in case obdays is empty, in which case obdays[1..-2] return nil. > I don't know why Array#[] with a range cannot just always return []. > > And don't worry about creating a new object, > obdays and obdays[1..-2] actually share storage.