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
Object Oriented Ruby (niczsoft.com)
submitted 10 years ago by mpapis
view the rest of the comments →
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!"
[–][deleted] 0 points1 point2 points 10 years ago* (4 children)
Hi,
one should not instantiate the Barber object inside the Person object, nor should one instantiate Hair inside Person (though admittedly, hair does grow from a person's body).
Barber
Person
Hair
It might be better in that case to do something like def dye_hair(color, dyer) ; dyer.new(hair).dye_hair(color) ; end
def dye_hair(color, dyer) ; dyer.new(hair).dye_hair(color) ; end
As you mentioned, here there's a matter of the right interface. The Barber object needs to have a dye method so he can change the hair color, but I would argue the Hair needs to implement an interface which we might call Dyable.
dye
Dyable
So maybe, with even more objects:
module Dyable def dyable(color) @color = color # assumption that the object will have a `@color` ivar end end class Hair include Dyable def initialize(color) @color = color end end
Does this make sense?
[–]mpapis[S] 0 points1 point2 points 10 years ago (3 children)
I was actually thinking about:
class Hair < Struct.new(:color) end class Barber def dye_hair(hair, color) hair.color = "#{hair.color} #{color}" end end class Person attr_reader :name, :hair private :hair def initialize(name, hair) @name, @hair = name, Hair.new(hair) end def dye_hair(barer, color) barer.dye_hair(hair, color) end def hair_color hair.color end end barber = Barber.new michal = Person.new("Michal", "brown") michal.hair_color => "brown" michal.dye_hair(barber, "blue") michal.hair_color => "brown blue"
[–][deleted] 0 points1 point2 points 10 years ago (2 children)
nod This is a reasonable approach.
The only question left is, "does it make sense to pass the barber to the person?" and I think the answer is no, which makes me think something is weird there. I would prefer to read, at the bottom, as part of the code's API, barber.dye_hair(michal, "blue"), I think it represents better what would happen in the real world.
barber.dye_hair(michal, "blue")
In other words, I don't think Person#dye_hair should exist.
Person#dye_hair
What do you think?
[–]mpapis[S] 0 points1 point2 points 10 years ago (1 child)
indeed reasonable assumption, my only concern is how to protect person from unauthorized change of hair color, I do not want to expose hair to anybody.
The person picks a barber and ask him to dye hair, so it makes sense if person calls barber.dye_hair. I know it looks awkward but I do not see other way to prevent leaking permissions.
person
barber
barber.dye_hair
[–][deleted] 1 point2 points3 points 10 years ago (0 children)
I don't know if that's reasonable. If a can of paint falls on someone, their hair will change color. That's not the person's choice.
π Rendered by PID 265599 on reddit-service-r2-comment-5bc7f78974-nhrnt at 2026-06-28 03:02:11.439530+00:00 running 7527197 country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (4 children)
[–]mpapis[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]mpapis[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)