问题
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?
回答1:
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."
回答2:
It's not 100% answer for this particular question, but could point to other useful trick.
In tests I'm doing it this way to trick RubyMine (5.0.2)
user = users(:normal) || User.new
since with fixtures I'm sure that the users(:first) will return the object, and because of Use.new - IDE thinks it should be User instance.
回答3:
It appears not. I'd recommend looking in the issue tracker for existing feature requests, and add make your voice heard there. For example:
http://youtrack.jetbrains.com/issue/RUBY-9142
Update
This feature is now shipping with RubyMine 7.0 (Tsubaki) EAP (138.1968) and higher (but note that Rubymine 7.0 is currently in EAP (i.e. beta) and there's always a chance this might not make it to the final distro.)
来源:https://stackoverflow.com/questions/10769307/can-i-tell-or-hint-to-rubymine-what-type-a-local-or-instance-variable-is