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:
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
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.Probably because the variable is never used, for the reasons detailed in (1)
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.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
Get Time Object at Start of Day in a Particular Time Zone
Passing Binding or Arguments to Erb from the Command Line
C1 or C2 Coverage Tool for Ruby
Using Will_Paginate Without :Total_Entries to Improve a Lengthy Query
How Rails Delegate Method Works
Actiondispatch::Http::Uploadedfile.Content_Type Not Being Initialized in Rspec Test
Can't Install Rmagick Gem on Ubuntu 13.04
Good Explanation of Ruby Object Model -- Mainly, 'Classes Are Objects'
What Are Tainted Objects, and When Should We Untaint Them
Parsing JSON Without Quoted Keys
How to Transfer Files Using Ssh and Scp Using Ruby Calls
How to Do Sti and Still Use Polymorphic Path Helpers
What Is the Purpose of a 'Transient Do' Block in Factorybot Factories
Elasticsearch Terms Aggregation by Entire Field
How to Make the Url's in Ruby on Rails Seo Friendly Knowing a @Vendor.Name