Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RubyMine - automatically detect .each, .map, and other iterator types

In RubyMine I can write

# @param [Array<MyClass>] things
def foo(things)
end

and RubyMine will autocomplete MyClass methods for things.first.*. However, when I loop through each, like:

# @param [Array<MyClass>] things
def foo(things)
    things.each { |t| t.* }
end

RubyMine loses its type inference. I know I can add comments to specify block parameter types, but looping through an object of some type should only yield parameters of that type anyway.

Is there any way I can write a custom rule for RubyMine so that .each, .map, and other iterators are presumed to have the type of the variable its called upon?

like image 839
jtmarmon Avatar asked Aug 22 '16 12:08

jtmarmon


1 Answers

From my research, it looks like you may be able to annotate like so:

# @param [Array<MyClass>] things
def foo(things)
  things.each {
    # @type [MyClass] t 
    |t|
    t.*
  }
end

Source: https://youtrack.jetbrains.com/issue/RUBY-9142#comment=27-787975

like image 96
BVB Avatar answered Nov 02 '22 23:11

BVB