0

I am new to ruby and rubmine. I am trying to use ruby mine.

I have class as follows

module Xyz;
    class A
     def doA()
     end
    end 
end 

module Xyz
  class B
    def doB()
    end
  end
end


module Xyz 

 class C
   define initialize(b)
     #injecting instance of Xyz::B into C
     @b = b
   end
   def doC()
     a = Xyz::A.new 
     a.doA()  #Autocompletes workshere
     b.doB()  #Doesnot autocomplete, so suggestions shown
   end
 end 
end

Why is my autocomplete not working for doB() ? Am i doing something wrong?or is it expected?

user93796
  • 18,749
  • 31
  • 94
  • 150
  • `Xyz::C#b` does not appear to be a defined method. Do you have an `attr_reader :b` or `attr_accessor :b` somewhere in class `C`? Either way it probably does not matter, because RubyMine would have no way of knowing that `Xyz::C#b` will return an instance of `Xyz::B`, so it can't autocomplete for you by guessing methods belonging to `Xyz::B`. – moveson Jun 09 '17 at 03:42
  • Xyz::C doesnot have any method or variable C. I am injecting instance of B into C .This instance is saved in @b inside C. Now i was looking for autocomplete when i type @b.somemethod . But as you said ruby mine has no way of guessing what the type of @b is, hence it cant suggest anything. Is my understanding correct?If yes, how do people code in ruby without this stuff.Can i and should i define type of b ?If there are no types i guess this is horrible. I am coming from java background and i love that :) – user93796 Jun 09 '17 at 05:13
  • I am not sure why my question is down voted.I guess most of the ruby beginners will have this doubt. Specially once coming from java background. When i started programming in java i had similar experience https://stackoverflow.com/questions/858572/how-to-make-a-new-list-in-java , i had asked a silly question which was downvoted. But now the same question has many upvotes – user93796 Jun 09 '17 at 05:14
  • 2
    If you can't live without strong typing then Ruby is probably not for you. – moveson Jun 09 '17 at 05:47
  • i know.But i have no choice for now, or else i would have never used Can u let me know how can i better use ide?Atom editor does slightly better then rubmine for me, it does suggestions.Any feedback? – user93796 Jun 09 '17 at 06:22
  • Ruby's a really dynamic language based on Smalltalk so it's very hard for your editor to infer exactly what's going on. Unlike Java which has very strict definitions for each object, Ruby tends to be composed on-the-fly, much to the consternation of anyone trying to write an auto-complete module. – tadman Jun 09 '17 at 07:05
  • 1
    @tadman: The interesting thing is that Smalltalk IDEs typically *can* do this, precisely *because* of Smalltalk's dynamism. The problem is that Ruby IDEs (and implementations) are *too static*. In Smalltalk, the program is always running, and you are editing the running program directly while it is running, so the IDE can just look at the objects to see which methods are there (and it can even trace the running code and see which message sends don't generate exceptions and thus catch stuff like `method_missing`). Whereas Ruby IDEs edit a dead text file. – Jörg W Mittag Jun 09 '17 at 08:43
  • @JörgWMittag Those Smalltalk editors usually run Smalltalk natively, its as integral as LISP is to EMACS, so you get deep insight into your objects. I don't know of many Ruby editors written with Ruby, or even any that actually run your code to discover the properties of it. Those that do support auto-complete usually do it using static analysis which is limited in its understanding. – tadman Jun 09 '17 at 09:14

1 Answers1

1

Look at this code:

class Test
   def initialize(a)
      @a = a
   end
end

Do you know what methods @a will have before it actually gets assigned and run? Ruby is truly duct typed language. RubyMine does a good job in indexing classes and predicting methods. For example this will work because Xyz has been indexed and RubyMine knows what methods it had, so it can predict it.

 a = Xyz::A.new 
 a.doA()  # Works because RubyMine KNOWS what class 'a' is
Ruslan
  • 1,919
  • 21
  • 42