-6

I have a definition of a function in Ruby like this:

def is_fdi?
 self.get_fed_state =~ /fdi/i ? true : false
end

Here is_fdi? is a function returning a Boolean. As far as I understand self.get_fed_state is a function call and its returned value is being compared with /fd1/i, but get_fed_state is not defined in the file anywhere.

Does anyone have any idea how is_fdi? is calling get_fed_state? Or, is there something that I should know to crack this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Max
  • 9,100
  • 25
  • 72
  • 109
  • 2
    `get_fed_state` likely **is** defined somewhere, if this code works. Is it an activerecord model? Also, that ternary operator is redundant. You can just use this: http://pastie.org/5487838 with the same effect – Sergio Tulentsev Dec 06 '12 at 08:54
  • @SergioTulentsev I have checked everywhere and this get_fed_state is not defined anywhre. what is activerecord model? – Max Dec 06 '12 at 09:12
  • Where did you get that code from? – Sergio Tulentsev Dec 06 '12 at 09:12
  • @SergioTulentsev I got it from my team itself, and it is a working code. I have been a given task of understanding a ruby project Do you think this would have something to do with self? – Max Dec 06 '12 at 09:14
  • i'm with sergio. can you confirm its not defined by doing `puts self.methods` and checking if its there? – sunnyrjuneja Dec 06 '12 at 09:50
  • @SergioTulentsev I even did grep -r ' get_fed_state' . , but did not get any definitions, dont know what else I can do. thanks anyway – Max Dec 06 '12 at 09:51
  • 1
    Do what I said and try some of the stuff from here: http://stackoverflow.com/questions/175655/how-to-find-where-a-method-is-defined-at-runtime – sunnyrjuneja Dec 06 '12 at 09:52
  • 2
    may be your team makes use of #method_missing - http://www.trottercashion.com/2011/02/08/rubys-define_method-method_missing-and-instance_eval.html - which would at least explain why you cant find that method anywhere in your project – krichard Dec 06 '12 at 10:01
  • `self` is a reference to the instance the methods gets called on – krichard Dec 06 '12 at 10:04
  • Maybe `fed_state` is defined, and some other mechanism is defining a getter for it? – Idan Arye Dec 06 '12 at 10:20

1 Answers1

1

Call the method. If you get a NoMethodError: undefined method it actually isn't defined. If it runs, then it is defined. It could be that it is defined in an external package the project uses, and not in the actual codebase.

Also you can shorten it like this:

def is_fdi?
 !!get_fed_state[/fdi/i]
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
AJcodez
  • 31,780
  • 20
  • 84
  • 118