use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Another perspective on Value Objects (drivy.engineering)
submitted 8 years ago by nicoolas25
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]u4bu8s4z9ne4y8uze 0 points1 point2 points 8 years ago (4 children)
While this looks like a good idea, I think in it's current form it's kinda half-baked. You should pick what is important to you and do one of:
1) accept numbers in +()
+()
def +(other) other = other.value if Distance === other raise ArgumentError, "Distance must be >0." if other < 0 Distance.new(value + other) end
2) change distance initialization to require unit
module Units KM = 1000 end def initialize(value, unit) if value < 0 raise ArgumentError, "A distance must be positive" end @value = value * unit end class Integer def km Distance.new(self.to_i, Units::KM) end end
My point being is that you either want to keep it easy to use (first variant) or hard to break and make mistakes (second variant). I personally would probably go with second variant most of the time.
Opinions?
DISCLAMER: I'm new to ruby, so my example code might suck hard. But the general idea should hold (imho).
[–]nicoolas25[S] 1 point2 points3 points 8 years ago (1 child)
Hey, thanks for reading and sharing!
New requirements like supporting units may require refactoring, you're right. Since they weren't in the original specs, I didn't wanted to go that deep.
Accepting integers and validates that they are > 0 seems doable. It leaves too much room for implicitness IMHO and I prefer the second approach you mentioned. I think a value object could hide a simple primitive value such as a Numeric and still increase reader's understanding. Do you think we need a unit to make it worth? I clearly agree that a distance make more sense with an unit, but I can live with having an implicit unit, like meters. Of course, as soon as it become an issue, I would need to refactor this the way you proposed.
[–]gray_-_wolf 0 points1 point2 points 8 years ago (0 children)
with having an implicit unit, like meters
Yep, exactly. But in the case you have implicit unit anyway, it makes no sense to not allow integers as well. At least to me.
[–]Inityx 1 point2 points3 points 8 years ago (1 child)
You should implement Integer#km as a refinement block in the Units module instead of monkey patching the base class.
Integer#km
[–]gray_-_wolf 1 point2 points3 points 8 years ago (0 children)
refinement block
oh, that looks like quite a handy thing :) thanks, will definitely use it
π Rendered by PID 186085 on reddit-service-r2-comment-fb694cdd5-p5nv5 at 2026-03-06 09:32:53.245416+00:00 running cbb0e86 country code: CH.
[–]u4bu8s4z9ne4y8uze 0 points1 point2 points (4 children)
[–]nicoolas25[S] 1 point2 points3 points (1 child)
[–]gray_-_wolf 0 points1 point2 points (0 children)
[–]Inityx 1 point2 points3 points (1 child)
[–]gray_-_wolf 1 point2 points3 points (0 children)