On Thu, Apr 14, 2011 at 12:49,  <redmine / ruby-lang.org> wrote:

> I think Numeric#within is perhaps less intuitive than something like Range#bound or Range#trim. It feels more natural to have it be a method on Range. I like Range#clamp but it might not be so obvious that restricts the argument to within the range; then again, (2..5).restrict(6) might lack some aesthetic appeal.

In my mind, Numeric#clamp is the right choice:

6.clamp(2..5) # ขอ 5
1.clamp(2, 5) # ขอ 2

class TimeColor
  def initialize(time)
    @red = (time * 1.5).clamp(0..0.25)
    @green = (time * 1.2).clamp(0..0.25)
    @blue = time.clamp(0..0.35)
  end
end

class TimeColor2
  def initialize(time)
    @red = (0..0.25).clamp(time * 1.5)
    @green = (0..0.25).clamp(time * 1.2)
    @blue = (0..0.35).clamp(time)
  end
end

TimeColor concentrates on the fact that the values are determined by
time, which is then clamped to specific ranges, whereas TimeColor2
concentrates on the fact that the values are clamped to specific
ranges, and only second that those values depend on time.

Each alternative has its merits, based on what you actually want to highlight.