Posts Tagged ‘Type Inference’

More Types Intelligence in RubyMine

Tuesday, November 11th, 2008

In the previous article covering type inference we’ve reviewed the following cases:

  • Type Inference for local variables
  • Detecting for index variable type
  • Understanding true, false and nil

But there’s more, much more…
Let’s see more intelligence RubyMine shows when editing Ruby code.

Unknown Method Calls

If in your code you make a method call or reference an attribute that RubyMine for some reason cannot resolve, you will be notified about it by the appropriate inspection but only once for this particular method and current variable. And your code will not look all red.

If you press Ctrl+B/⌘ B on such method call it will resolve to the first occurrence where inspection error is shown.

Custom Error Types

RubyMine correctly resolves your custom exceptions and their methods when you want to handle errors in your code.


class MyError < RuntimeError # custom class representing an error
  def foo
    #foo body
  end
end

begin
  # do_something
  rescue MyError => err
    err.foo
end

Code completion shows the methods and attributes of custom error class MyError.

(more…)

Type Inference in RubyMine

Friday, November 7th, 2008

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


if some_cond
  aaa = "string"
else
  aaa = [1, 2, 3]
end

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 string.rb.

(more…)