Project

fuzzy-ruby

0.0
No commit activity in last 3 years
No release in over 3 years
Auto-correct your Ruby code using fuzzy matching.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0

Runtime

~> 0.3.0
 Project Readme

Fuzzy Ruby

Build Status

One of the most difficult parts of programming is spelling everything correctly. Particularly in a dynamic-typed language like Ruby, a single typo can cause a critical runtime failure.

Traditional solutions to this problem include complex static analysis, burdonsome code reviews, and onerous unit testing.

Fuzzy Ruby is a much easier solution to this problem: it modifies the Ruby runtime environment so misspellings will automatically be corrected.

Example

Without fuzzy-ruby:

irb(main):001:0> "Hello".revers
NoMethodError: undefined method `revers' for "Hello":String
irb(main):002:0> my_variable = 123
=> 123
irb(main):003:0> my_vraiable
NameError: undefined local variable or method `my_vraiable' for main:Object

With fuzzy-ruby:

rirb(main):001:0> require 'fuzzy-ruby'
=> true
irb(main):002:0> "Hello".revers
=> "olleH"
irb(main):003:0> my_variable = 123
=> 123
irb(main):004:0> my_vraiable
=> 123

Installation

Add this line to your application's Gemfile:

gem 'fuzzy-ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fuzzy-ruby

Advanced Usage

A simple require 'fuzzy-ruby' will load Fuzzy Ruby and set it up to automatically correct misspellings. However, if you'd like more fine-grained control, you can use require 'fuzzy-ruby/api', which just gives you access to the FuzzyRuby api without modifying your runtime environment.

FuzzyRuby.install

Without a block, sets up Fuzzy Ruby to autocorrect misspellings until FuzzyRuby.uninstall is called. With a block, runs the block with Fuzzy Ruby installed, and then automatically uninstalls it when the block completes or raises an error. The return value or error from the block is propagated up.

FuzzyRuby.uninstall

Undoes a FuzzyRuby.install.

FuzzyRuby.mode = :autocorrect | :autocorrect_with_warning | :warn_only

Sets the mode of Fuzzy Ruby. By default, Fuzzy Ruby uses :autocorrect, which automatically corrects typos without warning. Other modes are :autocorrect_with_warning, which autocorrect but prints a warning to stderr, and :warn_only, which just prints the warning, but does not correct.

License

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

How Does It Work?

Fuzzy Ruby overrides method_missing on Object. This allows us to intercept methods calls to any object, as well as local variable access. We then grab the list of methods on the object, as well as the list of local variables (using the binding_of_caller gem) and select the best match using the Jaro-Winkler text distance algorithm (from the amatch gem). We then use eval or send to run the method or access the local variable.

Notes

Don't actually use this. Please.