0

Can I have an object reinitialize itself before a method is executed? I'm using Ruby and selenium to test a web app and I am trying to improve my page objects. for example

class Foo

  def initialize
    #stuff happens here 
  end

  def NewMethod
    self.initialize
    #What happens here is what I really want to happen 
  end 

end 

Is this a good or bad idea? Or is there a better way to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zach
  • 885
  • 2
  • 8
  • 27
  • Please improve your question by providing more details, it's not clear what is being asked here. – Mike Tunnicliffe Aug 30 '12 at 17:43
  • I'm trying to plan out how to make my page objects responsible for for what is on the page rather than checking for things in the test case. What I am trying to work out here is a strategy for making a page object of a navigation bar. I think I am now leaning in the direction of having all page objects that would have a nav bar on them inherit from a nav bar class. – Zach Aug 30 '12 at 20:01

2 Answers2

0

it's always possible to change the data contained in your object. you could ie put all the init-logic into an additional method and then call that from your custom methods.

in general, what you are trying to do does not sound like a good idea...

phoet
  • 18,688
  • 4
  • 46
  • 74
0

A remark in advance: methods are written in lower case. Please replace NewMethod with newmethod.

If you try Foo.newmethod you get an error.

What do you want to do? Do you want to define different possibilities to create an object?

What you could do:

class Foo
  def initialize
  end

  def self.newmethod
    me = self.new
    #Some actions ...
    me #Return the (modified?) object
  end 

end 

p Foo.newmethod #->#<Foo:0xb77d58>

Time is using something like this. There is Time.new, Time.gm, Time.local...

knut
  • 27,320
  • 6
  • 84
  • 112