14d99459914c594998d2506db1e868c2

Is there a one-liner to replace this with?

def ancestors
    ancestors = []
    x = parent
    while x 
      ancestors << x
      x = x.parent
    end
    ancestors.reverse
  end

Refactorings

No refactoring yet !

41508f4877e77d461357e6d3e956a1f8

Aaron Tinio

February 21, 2011, February 21, 2011 02:24, permalink

No rating. Login to rate!

modified from thinking-sphinx:

def ancestors_with_self
  (parent ? parent.ancestors_with_self : []) << self
end

def ancestors
  ancestors[0..-2]
end
41508f4877e77d461357e6d3e956a1f8

Aaron Tinio

February 21, 2011, February 21, 2011 02:27, permalink

1 rating. Login to rate!

ooops! this is the correct one:

def ancestors_with_self
  (parent ? parent.ancestors_with_self : []) << self
end

def ancestors
  ancestors_with_self[0..-2]
end
A8d3f35baafdaea851914b17dae9e1fc

Adam

February 21, 2011, February 21, 2011 20:08, permalink

No rating. Login to rate!

I'm assuming ancestors is an instance method?

def ancestors
  parent ? parent.ancestors + [ parent ] : []
end

Your refactoring





Format Copy from initial code

or Cancel