Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User = Struct.new(:name, :age, :profession)
- class MonoidBuilder
- def initialize(name: nil, age: nil, profession: nil)
- @name = name
- @age = age
- @profession = profession
- end
- def self.empty
- new
- end
- def +(other)
- self.class.new(
- name: other.name || name,
- age: other.age || age,
- profession: other.profession || profession
- )
- end
- def set_name(name)
- self + MonoidBuilder.new(name: name)
- end
- def set_age(age)
- self + MonoidBuilder.new(age: age)
- end
- def set_profession(profession)
- self + MonoidBuilder.new(profession: profession)
- end
- def == (other)
- name == other.name &&
- age == other.age &&
- profession == other.profession
- end
- def build
- User.new(name, age, profession)
- end
- protected
- attr_reader :name, :age, :profession
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement