On Jan 22, 9:40=A0am, Jason Lillywhite <jason.lillywh... / gmail.com>
wrote:
> But what if I have a bunch of methods in a class that all
> require the same exact arguments? My first thought was using initialize
> and instance variables:

This is a classic case of the Extract Class pattern (http://
www.refactoring.com/catalog/extractClass.html). It's a good first
thought to have, Jason. (Or on second thought, it'd be Extract Class
if this functionality were already present in a separate class. Is
there a refactoring pattern called "Use Instances of Already-Present
Class"? I purport this is a classic case of a pattern whose name I
can't think of right now.)

o< snip example >o

> But now I have to do area1 =3D Geom.new(4, 1.5, 2) and then area1.area
> Is this the only other way to do this:?
>
> Class Geom
> =A0 def Geom.area(b, y, m)
> =A0 =A0 a =3D (b + m * y) * y
> =A0 end
> =A0 def Geom.hyd_rad(b, y, m)
> =A0 =A0 r =3D (b + m * y) * y / (b + 2.0 * y * (1 + m**2)**0.5)
> =A0 end
> =A0 def Geom.wet_perim(b, y, m)
> =A0 =A0 p =3D b + 2.0 * y * (1.0 + m**2)**0.5
> =A0 end
> =A0 def etc...
> end
>
> I feel like I'm re-stating the arguments more than necessary. Could
> someone give me some pointers? Thank you!

So, as you realized, there's some trouble with only using instances of
a class. Here's one solution:

class Geom
  def initialize(b, y, m)
    @b, @y, @m =3D b, y, m
  end

  def area
    (@b + @m * @y) * @y
  end

  def hyd_rad
    (@b + @m * @y) * @y / (@b + 2.0 * @y * (1 + @m**2)**0.5)
  end

  def wet_perim
    @b + 2.0 * @y * (1.0 + @m**2)**0.5
  end

  class << self
    def area(b, y, m)
      new(b, y, m).area
    end

    def hyd_rad(b, y, m)
      new(b, y, m).hyd_rad
    end

    def wet_perim(b, y, m)
      new(b, y, m).wet_perim
    end
  end
end

I'm sure you see the pattern there. You have your instance methods
that can take advantage of storing away the variables upon
initialization, but you also have your class methods for convenience
if area1 =3D Geom.new(b, y, m).area offends you. And if the repetition
there is a concern, it's just a SMOMP away from cleanliness.

Hope that helps. Have a nice day.

Also, as a side note not entirely relevant to the topic at hand, I'm
not quite familiar with some of these variables and calculations.
Maybe it's been too long for me, but I can't figure out what kind of
area equation needs to have two multiplications. And I can't figure
out what b, y, and m stand for.

--
-yossef