Search code examples
ruby-on-railsrubyrubymine

Ruby and Rails code completion issues in RubyMine


Compared to the code completion I am familiar with from C# development, I find the code completion offered by RubyMine surprisingly incomplete.

  1. When I write a class with some methods and I want to call them from another class,
    why am I getting a code-completion list that is like "a mile" long, but with no relevant method-suggestions in it?
    ( Or is this a bug/feature in my RubyMine?! )

  2. Some classes like ActionMailer magically generate methods like the "deliver_*" methods ( see this example )... As I don't see them in code completion how am I supposed to know they even exist?
    ( Unfortunately I also get an error at the moment, that deliver_contact method does not exist... As I completely copied from the example am asking myself now wheter this feature still exists 8[ )

Are they any ways to fix these issues?


Solution

  • It's not that Ruby wasn't "designed" to be used with code completion, it's that behavior can be added in a variety of ways, including during program execution. This makes code completion hard. RubyMine does a good job, but only for obvious or known functionality, when an object's type is known.

    http://apidock.com provides reference material for Ruby, Rails, and RSpec. Playing around in irb/pry can help a lot. You can always get methods on a class/instance by eval-ing foo.methods in a REPL (although I almost always foo.methods.sort).

    That list can be filtered if you "sort of" know what you're looking for with find/grep, e.g.

    > "foo".class.instance_methods(false).sort.grep /each/
    => [:each_byte, :each_char, :each_codepoint, :each_line]
    

    In the Rails environment, running rails console (I prefer using pry for the console, YMMV) gives you access to your environment, including exposing a lot of the dynamic methods. Note that some methods do not exist until they are called for the first time (notably those of the find_by_foo_and_bar variety) so some functionality may still be hidden.