Last Updated: September 04, 2018
·
847
· blazeeboy

Forgiving ruby

sometimes when you write in ruby fast enough you may miss some character, maybe miss write a character or something, i thought that there is a way we can make ruby more forgiving toward your writing mistakes
it is easy we'll monkey patch the Object class to respond to methods that doesn't exists but there is another method that is quite like it.

Gist : https://gist.github.com/11158374

#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)

# monkey batch, yup that a bad practice,
# but lets say that this is a proof of concept
# 
# we'll open the object class and handle the 
# method missing situation, we'll claculate
# distance between the requested method and 
# all methods available in object, then the nearest
# method if distance doesn't exceed certain number
# then execute it.
class Object
  def method_missing(meth, *args, &block)
    threshold = 3
    all_meth = methods.sort
    all_meth.sort_by! do |m| 
      string_distance m, meth
    end
    if string_distance(all_meth.first, meth) <= 3
      send all_meth.first, *args, &block
    else
      super
    end
  end

  # we'll calculate distance between 2 string by
  # getting number of characters in 1 and not in 2
  # and number chars in 2 not in 1, sum the two
  # differences and return that weight
  # less weight is more similar method
  def string_distance(str1, str2)
    one_way = str1.to_s.chars - str2.to_s.chars 
    the_other_way = str2.to_s.chars - str1.to_s.chars 
    one_way.size + the_other_way.size
  end
end

# ## UseCase ?
# you can use `nil` instead of `nil?`
p "26512135".nil
# you can use `toi` and `tof` instead of `to_i` and `to_f`
p "12123".tof

# this way ruby will be more forgiving if you wrote
# method name with wrong character or less character or more 
# with 1 character, when you increate the `threshold` it'll
# be more forgiving :D, 
# 
# happy coding.

7 Responses
Add your response

Well, I don't think that this is a good idea. IMHO developers should be warned about miss writing because ruby is forgiving too much by not forcing us to use semicolons, parentheses and so on...

over 1 year ago ·

that is right, the above is a only a proof of concept that this is possible not a production technique

over 1 year ago ·

Sounds like gigantic security vulnerability to me. :)

over 1 year ago ·

Good tip though! There should be a use-case for this technique.

over 1 year ago ·

I use Linter with Sublime and Atom. It points out programming-typo's when you type stuff. combined with Rubocop, etc. it makes a perfect way of correcting your mistakes.

over 1 year ago ·

I don't see this like a good idea for production code, but it looks super fun! It remembers me the autocorrection for commands in zsh and I think this could be useful for debugging:

What if you combine this idea with a call to ruby debugger, write a warning in the log or sending a report to your favourite exception notificator? If you have spent a while getting ready some kind of test that is time consuming, may be a largue test suite or trying some commands in the console, you'll be super-angry if something fails because you mistyped a 'nil?' call and you have to start over. I think it could be useful to be aware of the mistake but don't stopping the execution of your test because of this. What do you think? You can always deactivate it for production environment ;-)

over 1 year ago ·

it looks like someone already implemented the idea as a gem even before i come up with
http://ruby-jokes.github.io/close_enough/

over 1 year ago ·