I found that code in the actual codebase of a web app I'm working on. What do you think about that? def is_a_feature? self.featured? end def is_not_a_feature? !self.featured? end I just want to know who written the second method, just for curiosity... El
on 2013-02-15 12:25
on 2013-02-15 14:40
On Fri, Feb 15, 2013 at 12:25 PM, Elr0ndK Asda <lists@ruby-forum.com>
wrote:
> I just want to know who written the second method, just for curiosity...
Why do you ask here who wrote the second method? How could we possibly
know?
Cheers
robert
on 2013-02-15 14:49
Elr0ndK Asda wrote in post #1097056: > I found that code in the actual codebase of a web app I'm working on. > What do you think about that? > I don't know - you might expect something from the below to explain you,which is not clear to us. Please make it specific .. > def is_not_a_feature? > !self.featured? > end > > I just want to know who written the second method, just for curiosity... > > El
on 2013-02-15 17:22
The second one has been written only for a conditional validation (in the same model), example: - validate_presence_of :field, :if => :is_not_a_feature? But Ruby have also the unless, so it seems to me absolutely unuseful. In addition, I'm not expert of Rails and Ruby, but I think also that the first method is redundant: featured is a boolean field of the related database table and in my thought, adding an attr_reader for the featured field, there are no needs of other methods.
on 2013-02-15 17:46
On 15.02.2013 16:22, Elr0ndK Asda wrote: > featured is a boolean field of the related database table and in my > thought, adding an attr_reader for the featured field, there are no > needs of other methods. It's quite common in Ruby to alias methods if that improves readability. Whether you consider the additional code worthwhile seems to me a pretty subjective call. Generally I would agree with you though that generating a whole new method just for negation seems a bit OTT. I would also rewrite the first method using alias to make it even more obvious that that is all it is.
on 2013-02-15 18:26
On Fri, Feb 15, 2013 at 5:46 PM, Alex Gutteridge <alexg@ruggedtextile.com> wrote: > It's quite common in Ruby to alias methods if that improves readability. > Whether you consider the additional code worthwhile seems to me a pretty > subjective call. Generally I would agree with you though that generating a > whole new method just for negation seems a bit OTT. I would also rewrite the > first method using alias to make it even more obvious that that is all it > is. Absolutely agree! I also find the "is_a_" prefix ugly - usually where in Java you have isFeature() in Ruby you just have feature?. And given that doing alias feature? featured? doesn't really sound worthwhile I'd probably scrap both methods. Kind regards robert
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.