Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rubymine

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?

like image 842
WiseOldDuck Avatar asked May 26 '12 19:05

WiseOldDuck


People also ask

How do you distinguish between instance variables and local variables for a method?

The main difference between instance variable and local variable is that instance variable is a variable that is declared in a class but outside a method, while a local variable is a variable declared within a method or a constructor.

Can you declare a local variable in a method that has the same name as an instance variable in the class?

A static method in a class can access the class variables in the same class. You can declare a local variable in a method that has same name as an instance variable in the class. You can declare variables of the same name in a method even though they are in the same block.

How do you define a local variable in Ruby?

Local Variables: A local variable name always starts with a lowercase letter(a-z) or underscore (_). These variables are local to the code construct in which they are declared. A local variable is only accessible within the block of its initialization. Local variables are not available outside the method.


3 Answers

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."

like image 193
WiseOldDuck Avatar answered Sep 19 '22 20:09

WiseOldDuck


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.

like image 33
Grzegorz Avatar answered Sep 22 '22 20:09

Grzegorz


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.)

like image 39
Matt Connolly Avatar answered Sep 23 '22 20:09

Matt Connolly