📜 ⬆️ ⬇️

Method Finder for Ruby by Squeak's Motive

After the post user aovoe wanted something similar for ruby. Hastily, a code was thrown that can search by selectors without arguments (for example, “hello” .upcase).
Examples:

   irb (main): 001: 0> "hello" .suggest_method ("HELLO")
   => ["upcase", "swapcase"]
  
   irb (main): 002: 0> "hello" .suggest_method ("Hello")
   => "capitalize"
  
   irb (main): 007: 0> Date.parse ('13 Sep 2009 '). suggest_method (256) # Programmer's Day
   => "yday"
  
   irb (main): 009: 0> 1.01.suggest_method (1)
   => ["prec_i", "round", "truncate", "to_i", "to_int", "floor"]
  
   irb (main): 010: 0> 1.01.suggest_method (2)
   => "ceil"


MethodFinder can take into account several results, for example:

   irb (main): 011: 0> MethodFinder.suggest_method (-1.01 => -2, 1.01 => 1)
   => "floor"

   irb (main): 001: 0> MethodFinder.suggest_method (-1.01 => -1, 1.01 => 1)
   => ["to_int", "round", "to_i", "prec_i", "truncate"]

')
Update:
Now and on selectors with arguments

   irb (main): 005: 0> "hello" .suggest_method ("hello world", "world")
   => ["+", "concat", "<<"]
  
   irb (main): 003: 0> MethodFinder.suggest_method ([2, 4, 2], [[2], [2, 2], 2])
   ./method_finder.rb:20: warning: do not use Fixnums as Symbols
   => "*"

Sorts on githaba: github.com/mholub/method_finder/tree/master

Update:
This is a bicycle, because a lot of people have already written analogs, for example, andy@nobugs.org
www.nobugs.org/developer/ruby/method_finder.html
Improved version:
redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html

Source: https://habr.com/ru/post/55544/


All Articles