Search code examples
rubyrubymine

Can I tell or hint to RubyMine what type a local or instance variable is?


I'm trying to leverage the RubyMine quick-docs and code completion. I was pleased to discover how well it integrated the YARD-style comments:

# @param [Numeric] width
# @param [Array<String>] values
# @return [Widget]      
def foo(width, values)

... these comments work great for parameters, return-types, even typed collections. But I can't find any similar tags for instance or local variables, and am pretty sure there's no type casting available in Ruby (Did I mention I'm new to this?)

Is there any way to clue RubyMine in to the types of local and/or instance variables?


Solution

  • It appears this is forthcoming, based on a recent comment posted to the issue tracker referenced by Matt Connolly: http://youtrack.jetbrains.com/issue/RUBY-9142#comment=27-787975

    "local variables can be annotated with or without variable name:"

    # @type [String]
    my_var = magic_method
    
    # @type my_var [String]
    my_var = magic_method
    
    # @type [String] my_var
    my_var = magic_method
    
    # @type [String] my_var And some documentation is allowed
    my_var = magic_method
    

    "Also multi-assignments are supported:"

    # @type my_var [String] The first part
    # @type other_var [Range] The second part
    my_var, other_var = magic_method
    

    "But in the case of a multi-assignment the form without var name would not work (this is arguable, but I incline to that it may lead to some errors)

    Also block parameters can be annotated:"

    method_with_block do
      # @type [String] param1
      # @type [Range] param2
      | param1, param2 |
      # some code...
    end
    

    "The thing to note is that type annotations are to be placed after do or { and before block parameters list, to help avoiding probable ambiguity. In the case of one-liners it looks cumbersome, but I am not sure they are to be heavily annotated. Anyway, any suggestions are very welcome."