How to Tell or Hint to Rubymine What Type a Local or Instance Variable Is

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

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

Pycharm Type-Hinting of Class Fields / Instance Variables

First off, as one of the commenters on your question pointed out, using upper_case variable names confuses them with Classes/Types. If you look at your question, you'll see it even confused the Stackoverflow code-formatter.

Additionally, in your Album constructor, you are masking the global variable "Photo" with a local parameter. That might confuse PyCharm, especially when you do the following. So for my answer + test below, I renamed your parameter to a lower-case photo so that it doesn't interfere.

**Edit: ** I did find a better way. See "The Right Way".

First Attempt

class Album:
def __init__(self, name, photo, next):
self.name = name
self.photo = self.get_photo(photo)
self.next = next

def get_photo(self, photo):
"""
@rtype: Photo
"""
return photo

def __str__(self):
return "Album name is: " + self.name

The way it works is it uses the PyCharm type-inference. See here for details on how it works.

And below a screenshot of it working:

Image of answer working

Note: I do not recommmend doing this, as it is a hack. I came across your question as I'm attempting to find out if there is a better way of doing this in PyCharm.

The Right Way

The right way of doing this is to give PyCharm the type of the variables in the constructor. Essentially, moving the type-binding from the method as above, and into the constructor as part of the docstring entry "type".

class Album:
def __init__(self, name, photo, next):
"""
@type name: str
@type photo: Photo
@type next: str
"""
self.name = name
self.photo = photo
self.next = next

def __str__(self):
return "Album name is: " + self.name

class Photo:
def __init__(self, name, caption, Tag, next):
self.name = name
self.caption = caption
self.Tag = Tag
self.next = next

photo1 = Photo("name.txt", "caption", "tag", "next")

album1 = Album("TestAlbum", photo1, "next")

album1.photo.#code completion working here

Ruby: Undefined method

  1. It has to do with scopes. A method body runs in the scope/context of the instance, but the class definition runs in its own scope. You could use instance variables and initialize the hash as @directions in the initialize method, but in your case the hash will probably not change, so I recommend using a constant. In Ruby these are declared just like variables, but when the first character is uppercase, they are constants.

  2. Probably because the variable is never used, for the reasons detailed in (1)

  3. key is also never used, you can ignore it by using a variable name that starts with an underscore (_key) or just an underscore will also do. I like to use the first form, so I know what's in there when I come back later to this piece of code.

  4. It probably wants you to convert it to a block because of better readability.

Full code:

class Die
DIRECTIONS = {

north: 1,
south: 2,
east: 3,
west: 4
}

def throw
direction = DIRECTIONS.select do |_key, value|
value == rand(4)+1
end
puts direction
end
end

Where is scaffold in RubyMine

Whenever searching for something in RubyMine, a highliy recommented thing is "Search"->"Find Action" (Cmd-Shift-A).

Just type "scaffold" in the search field and it shows the relevant IDE actions.



Related Topics



Leave a reply



Submit