Type Inference in RubyMine
What is the one thing that some of us like and others dislike about dynamic languages such as Ruby? It is dynamic types of course! It is a blessing and a curse. And for an IDE that claims being a Ruby IDE it is a serious task. There is a lot an IDE can help developer when working with such extremely flexible type system as Ruby’s.
We think RubyMine is the leader here. And here is why.
Local Variables Type
aaa = “string”
aaa = [1, 2, 3]
aaa. #at this point aaa is either string or array
RubyMine knows that
aaa can be one of two types, so pressing Ctrl+Space for code completion shows the list of methods corresponding to possible types.
The “…” on the right means there are more than one variations.
To see the inferred types that RubyMine sees for the variable press Ctrl+Q/^ J for Quick documentation lookup.
If we choose
include?(obj) and then press Ctrl+B/⌘ B for method definition, RubyMine will confirm whether to go to
array.rb or to
for Index Variable Type
If you need to do something with the
for loop index variable, RubyMine knows its type and suggests the appropriate methods.
range = true ? 1..10 : -10..1
for i in range do
Seeing true, false and nil
When it comes to detecting the truth RubyMine is at its best. It helps working with logical types by suggesting correctly for both
There is also a special
nil object in Ruby which RubyMine knows well and treats correctly.
daynmb = 4 # daynum – nubmer of day of week
(1..5).include?(daynmb) ? weekend = false : weekend = true
to be continued: detecting
Rescue type, Rails type inference and some more in the next publication…
Subscribe to Blog updates
Thanks, we've got you!
RubyMine 2020.3.4 and 2021.1.1 introduce the concept of trusted projects. It is designed to reduce the risks associated with opening projects from unknown or untrusted sources. Check out this blog post to learn more. As you might already know, when you open a project, RubyMine runs various tools lik…
RubyMine Navigation: Recent Locations PopupEarly Access Program
RubyMine 2019.1 is filled with features and options to help you navigate around your projects quickly without a mouse or touchpad. For example, you can quickly switch between tool windows, go to classes or actions, and open recently edited files. In our Navigate in RubyMine Like a Pro blog post, we …
How to work with Docker/Docker Compose from RubyMineEarly Access Program
To get the most recent information about Docker support in RubyMine, refer to our documentation: Tutorial: Docker Compose as a remote interpreter Tutorial: Docker as a remote interpreter RubyMine 2019.1 has a bunch of capabilities which allow you to work with Docker and Docker Compose. You can i…
Profile Ruby & Rails Apps With rbspy in RubyMineEarly Access Program
RubyMine 2019.1 adds support for rbspy – a great sampling profiler for Ruby (many thanks to @jvns!). This post will help you start profiling your Ruby/Rails apps with RubyMine in 5 minutes. Here are the exact steps: Identify the scenario to be analyzed Create a profiler configuration Run…