J
Jason Lillywhite
I have a design question for a beginner:
what is better for my case?
I want to access functionality from a library I created.
I've tried class methods like this:
class Calc
def Calc.velocity_head(v)
v**2/(2.0*G)
end
def Calc.pressure_head(p)
h = p / Gamma
end
def etc...
end
This is nice because now I can create many "velocity_head" objects or
"pressure_head" objects as needed by doing vh1 = Calc.velocity_head(4.5)
for example. 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:
Class Geom
def initialize(b, y, m)
@b, @y, @m = b, y, m
end
def area
@a = (@b + @m * @y) * @y
end
def hyd_rad
@r = (@b + @m * @y) * @y / (@b + 2.0 * @y * (1 + @m**2)**0.5)
end
def wet_perim
@p = @b + 2.0 * @y * (1.0 + @m**2)**0.5
end
def etc...
end
But now I have to do area1 = Geom.new(4, 1.5, 2) and then area1.area
Is this the only other way to do this:?
Class Geom
def Geom.area(b, y, m)
a = (b + m * y) * y
end
def Geom.hyd_rad(b, y, m)
r = (b + m * y) * y / (b + 2.0 * y * (1 + m**2)**0.5)
end
def Geom.wet_perim(b, y, m)
p = b + 2.0 * y * (1.0 + m**2)**0.5
end
def etc...
end
I feel like I'm re-stating the arguments more than necessary. Could
someone give me some pointers? Thank you!
what is better for my case?
I want to access functionality from a library I created.
I've tried class methods like this:
class Calc
def Calc.velocity_head(v)
v**2/(2.0*G)
end
def Calc.pressure_head(p)
h = p / Gamma
end
def etc...
end
This is nice because now I can create many "velocity_head" objects or
"pressure_head" objects as needed by doing vh1 = Calc.velocity_head(4.5)
for example. 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:
Class Geom
def initialize(b, y, m)
@b, @y, @m = b, y, m
end
def area
@a = (@b + @m * @y) * @y
end
def hyd_rad
@r = (@b + @m * @y) * @y / (@b + 2.0 * @y * (1 + @m**2)**0.5)
end
def wet_perim
@p = @b + 2.0 * @y * (1.0 + @m**2)**0.5
end
def etc...
end
But now I have to do area1 = Geom.new(4, 1.5, 2) and then area1.area
Is this the only other way to do this:?
Class Geom
def Geom.area(b, y, m)
a = (b + m * y) * y
end
def Geom.hyd_rad(b, y, m)
r = (b + m * y) * y / (b + 2.0 * y * (1 + m**2)**0.5)
end
def Geom.wet_perim(b, y, m)
p = b + 2.0 * y * (1.0 + m**2)**0.5
end
def etc...
end
I feel like I'm re-stating the arguments more than necessary. Could
someone give me some pointers? Thank you!