How can I overwrite class variables

Hi,

How can I overwrite a class variable? What I have here is a class
called Machine which calculates how many times a rectngle fits in a 60 x
120 rectangular shape and its working, in the code below it could fit 36
times, the problems is that I’m going to have differnet methods which
are supose to overwrite @@kerf and make the calculation based on these
new values but I do not know how to overwrite the class variable @@kerf.

How can I overwrite variable @@kerf so that when I call method "laser
(machine1.laser) the result would be 6 instead of 36 and when the method
“rurret” (machine1.turret) is called the result would be 12 instead of
36?


class Machine
@@kerf = 0
def initialize(prt_w, prt_l)
part_width = prt_w + @@kerf
part_length = prt_l + @@kerf
sheet_width = 60
sheet_length = 120
parts_y = sheet_width / part_width
parts_x = sheet_length / part_length
@total = parts_y * parts_x
end

def laser
@@kerf = 20
puts “#{@total.floor}”
end

def turret
@@kerf = 10
puts “#{@total.floor}”
end

end

machine1 = Machine.new(10 , 20)
machine1.laser
machine1.turret

Sorry if my code doesnt make any sense but I’m practicing.

Thanks a lot for your help!

To be honest you do not need class variables for this and you should
not do the calculation in the initialize method as you do not know
what value to use for kerf. This is a better solution.

class Machine
def initialize(part_width, part_length)
@part_width = part_width
@part_length = part_length
end

def laser
puts calc(20)
end

def turret
puts calc(10)
end

private

def calc(kerf)
sheet_width = 60
sheet_length = 120

parts_y = sheet_width / (@part_width + kerf)
parts_x = sheet_length / (@part_length + kerf)

return parts_y * parts_x

end
end

machine1 = Machine.new(10 , 20)
machine1.laser
machine1.turret

Wow! I knew someone would come with a better solution. Awesome!

I’m not trying to improve your code I’m just trying to practice by
overwriting variables (in this case an instance variable not a class
variable).

class Machine
def initialize(part_width, part_length)
@part_width = part_width
@part_length = part_length
end

def laser
@kerf = 20
calc
end

def turret
@kerf = 10
calc
end

private

def calc
sheet_width = 60
sheet_length = 120

parts_y = sheet_width / (@part_width + @kerf)
parts_x = sheet_length / (@part_length + @kerf)
 puts parts_y * parts_x

end
end

machine1 = Machine.new(10 , 20)
machine1.laser
machine1.turret

This makes me feel good and bad, good because its increible to see
people helping others and whats best see people that identify the
problem in a metter of seconds (you guys are awesome) and bad becuase I
could’t figure this out by my self :frowning:

Thanks a lot

Marc H. wrote in post #1031087:

You just need to write more ruby code.

Ideally, write small classes that solve a given problem,
then re-use those classes to make a larger project.

Do you know of any tutorials where you can actually practice Ruby in
actual projects? Not tutorials on the Ruby syntax (projects using Ruby)

I have another question, in Peters code he used “private” but if I
remove it, it actually works can someone explain the use of private
here, is this the way of making a private def in Ruby? Meaning that this
def will not be accessible outside the class.


private

def calc
sheet_width = 60
sheet_length = 120

parts_y = sheet_width / (@part_width + @kerf)
parts_x = sheet_length / (@part_length + @kerf)
 puts parts_y * parts_x

end


There are two ways (maybe more) to define methods as private.

class C
private # every method following will be private
def m1 … end
def m2 … end
end

class C
def m1 … end
def m2 … end
private :m1, :m2
end

In both cases, m1 and m2 would be private.

Of course, in Ruby nothing is fixed. You can make private methods public
again if you really want to.

-Jingjing

bad because I could’t figure this out by my self

You just need to write more ruby code.

Ideally, write small classes that solve a given problem,
then re-use those classes to make a larger project.

On Nov 9, 2011, at 5:29 PM, Fily S. wrote:

I have another question, in Peters code he used “private” but if I
remove it, it actually works can someone explain the use of private
here, is this the way of making a private def in Ruby?

You make methods private if you don’t want to expose them on the
object’s public API. Note that because of Ruby’s dynamic nature,
‘private’ does not mean that you can’t call the method from the
outside, it just discourages it. Ruby enforces that private methods can
only be called without an explicit receiver, i.e., you can not call it
with a dot directly in front of it. If ‘calc’ is a private method of
Class A, then you cannot do this:

A.new.calc # => fails

However, inside of an instance of A, you can call calc without
specifying a receiver (because self will be used implicitly). If you
still want to call a private method from the outside, the common way is
to use #send. Therefore, you could do this:

A.new.send(:calc)

Typically, you will make methods private if you don’t intend them to be
called from the outside. Use them to express your intentions more
clearly and help others (or your future self) to understand your code
better.

Thank you all very much for the good information!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs