Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class_eval and open classes

Tags:

ruby

spree

I am using Spree, and Spree has a class called Order that looks like:

module Spree
  class Order
    # class definition.
  end
end

In my own app, I have been customising Order like so:

Spree::Order.class_eval do
  # customisations
end

My question is, can I simply just do this:

module Spree
  class Order
    # My own customisations.
  end
end

Any downsides to this? Essentially, I want to avoid using class_eval.

like image 232
Benjamin Tan Wei Hao Avatar asked Mar 10 '14 02:03

Benjamin Tan Wei Hao


1 Answers

Benjamin, reopen class will not inform you (but class_eval will raise error) if the existing class does not exist or not loaded.

But if you have test coverage, reopen class should be safe I guess?

See https://stackoverflow.com/a/900508/474597 for more detailed explanation.

like image 57
Huiming Teo Avatar answered Nov 05 '22 06:11

Huiming Teo