I've come across some code in a Rails app in the form of
ThirdPartyLibrary::Foo.class_eval do
def bar?
@bar_field == 'true'
end
end
and I'm wondering why they didn't just do
class ThirdPartyLibrary::Foo
def bar?
@bar_field == 'true'
end
end
Are there any advantages in using class_eval when there isn't anything you want to pass in to the new code?
class_eval 'adds' something to an existing class and your second example just defines the class new and overwrites everything that was there before. This is used for example when you want to monkeypatch or extend libraries.