Ruby Woes - Can't Modify Self

I’ve always liked to think that anyone can change if they want to badly enough.  After about half an hour of searching, I discovered that Ruby objects, within the context of class methods, cannot.  I was looking to extend the Numeric class (this is something really cool you can do in Ruby) to add a method that would “clamp” the number to be within a certain range, between a minimum and a maximum value.  For example:

5.clamp(-2, 2) # => 2
-20.clamp(-2, 2) # => -2
1.clamp(-2, 2) # => 1`

This function is easy enough to write.

class Numeric
  def clamp(minimum, maximum)
    return minimum if self < minimum
    return maximum if self > maximum
    return self`
  end
end

But I also wanted to create a bang version of the function, that is a clamp!() function that would modify the receiver and actually change its value, like this:

x = 5 # => 5
x.clamp!(-2, 2) # => nil`
x # => 2`

What I learned is that this isn’t possible. You can’t even create a method that modifies the self variable without getting an exception from Ruby. Just wanted to share that bit of knowledge, because it seems like there’s not much documentation out there about it.

About Adam Platt

Adam Platt is a technologist with more than a decade of experience across the full stack. His passion for technology and penchant for rendering complex technical ideas into simple terms have made him an in-demand speaker. His resume includes BriForum, the PowerShell Summit, teaching engagements and more.

He is one of the 10 types of people who understand binary and he can solve a Rubik’s Cube.

About Adam Platt

Adam Platt is a technologist with more than a decade of experience across the full stack. His passion for technology and penchant for rendering complex technical ideas into simple terms have made him an in-demand speaker. His resume includes BriForum, the PowerShell Summit, teaching engagements and more.

He is one of the 10 types of people who understand binary and he can solve a Rubik’s Cube.