Pred

This creates methods #pred and #pred! to target multiple base Objects. The plan is to include this gem in future upgrades on several gems I am working on. This basically implements the inverse of #succ and #succ!, which will be imported on the appropriate basic ruby types. The goal is to create a Range super class that works forwards as well as backwards. This extended Range is needed on an Array super class that uses lists of integers, ranges, and backward ranges to work within the context of the access operators :[] and :[]=. I expect this to snowball on other projects as well.

Installation

Add this line to your application's Gemfile:

gem 'pred'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pred

Usage

This method updates String, Integer, and Symbol to include #pred as one of its instance methods. The #pred method is simply the inverse of #succ. The method #pred! is implemented only on String due to its mutating abilities. Integer is trivial, but String is a mother bear. There are many tricky edge cases on how #succ works, and finding an inverse operation proved harder than first glance. See the code below to see why:

"az".succ       # "ba"
"a__z".succ     # "b__a" ... "b__a".pred should give me "a__z"
"z_z".succ      # "aa_a" ... inserts carry-over at front of string
"0_z".succ      # "0_aa" ... ignores digits if separated by underscore
"a9z".succ      # "b0a"  ... but if contiguous, rolls over as expected 
"a99z".succ     # "b00a" ... things look non-numeric here
"9z".succ       # "10a"  ... looks more numeric here as 9+1==10
"az_Z9Z_z".succ # "ba_A0A_a"  ... numbers touching letters work as expected
"az9_zZ9z".succ # "az9_aaA0a" ... but not if they touch something else
"a_9z".succ     # "a_10a"     ... either side breaks the connection
"`".succ        # "a"         ... edge case "a".pred should equal "`"

There are even edge cases like: t_string.succ == '!a!' where t_string does not exist. Normal use should not run into such cases however.

Now we have a balance of force in the ruby universe as #pred is the ying of the yang #succ. The name pred is shorthand for predecessor as succ is shorthand for successor. If you create the method #succ on one of your classes, you should also create its counterpart #pred.

Revision History

  • Version 0.1.2 fixed bug on edge case '!a!.pred
  • Version 0.1.1 fixed bug on edge case 'a'.pred

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install.

Contributing

I need to control this for the time being

License

The gem is available as open source under the terms of the MIT License.