From: ara.t.howard@... Date: 2006-10-27T04:29:36+09:00 Subject: Re: Beginner would like help with oo-modelling On Fri, 27 Oct 2006, Pa Per wrote: > Ok, now I understand what you mean. And you are right, for some reason I > totally got off the path I was following initially. But the problem I'm > trying to deal with right now would probably persist when storing the > information inside an object: > > I'm currently using the num.step method to iterate from zero to the length > of the ruler, in order to calculate the positions. This creates multiple > types of lines on the same position, e.g. > > | | | lines[0] > | | | | | | lines[1] > |||||||||||||||||||||||||||| lines[2] > 0 5 10 > > lines indicate value, e.g. lines[2] = [1..n] > > To prevent that, when adding a line (position) to lines, it is necessary to > iterate over the previous array(s), checking if the value-to-be is already > included (enum.include?). > > I don't like that approach, although it probably works. I have yet to > experiment with "following the metaphor". > > Alternatively, I would have to find a method to generate the positions > directly, like > 0, 10, 20, 30 > 5, 15, 25, 35 > 1,2,3,4,6,7,8,9, ... > > But since the pattern depends on the parameter, I am not sure if a > reasonably short equation could be found. it's quite short: harp:~ > cat a.rb class TickGen include Enumerable def initialize a, step, b = nil @a, @step, @b = a, step, b end def each n = @a loop{ yield n n += @step break if @b and n > @b } end def include? tick q, r = (tick - @a).divmod @step r.zero? and (@b.nil? or q <= @b) #=> THE EQUATION end def to_a raise "infinitly big array" unless @b super end def to_ary() to_a end def | other (to_a | other.to_a).sort.uniq end end a = TickGen.new 0, 2, 10 p a.to_a p a.include?(8) b = TickGen.new 1, 1, 10 p b.to_a p b.include?(8) c = TickGen.new 1, 2, 10 p c.to_a p c.include?(8) p( a | b | c ) harp:~ > ruby a.rb [0, 2, 4, 6, 8, 10] true [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] true [1, 3, 5, 7, 9] false [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] regards. -a -- my religion is very simple. my religion is kindness. -- the dalai lama