Blog » Blog Entry

Get previous integer in Ruby

February 1, 2008

There's a next and succ that do the same thing, but sadly Ruby has no prev. Here how you can add it:

class Integer
  def prev
    self - 1
  end
end

> irb
>> 1.prev
=> 0
>> 2.prev
=> 1
Tags: ruby

« Stop it, you're killing me.. RailsDB 0.2 »

Add a comment:

Title:

Comment:

Name:

Email: