Skip to main content
added 11 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25

Notes:

  • I am afraid I willI'd propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add just one usingmethod and use symbols for the days? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes, trees, or sets, not masks of powers of 2 :-) Here I'd use a set.

  • I prefer functional solutions even with OOP, just return new instances instead of modifying the existing ones. Referential transparency, idempotence, all the goodies follow.

I'd write (orientative, you get the idea):

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days.union(names))
  end
end

Notes:

  • I am afraid I will propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add one using symbols for the days? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes, trees, or sets, not masks of powers of 2 :-) Here I'd use a set.

  • I prefer functional solutions even with OOP, just return new instances instead of modifying the existing ones. Referential transparency, idempotence, all the goodies follow.

I'd write (orientative, you get the idea):

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days.union(names))
  end
end

Notes:

  • I am afraid I'd propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add just one method and use symbols for the days? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes, trees, or sets, not masks of powers of 2 :-) Here I'd use a set.

  • I prefer functional solutions even with OOP, just return new instances instead of modifying the existing ones. Referential transparency, idempotence, all the goodies follow.

I'd write (orientative, you get the idea):

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days.union(names))
  end
end
added 34 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25

Notes:

  • I am afraid I will propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add one using symbols for each task, and take/returnthe days as symbols? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes, trees, or sets, not masks of powerpowers of 2 :-) Here I'd use a set.

  • I prefer functional solutions even with OOP, you just return new instances instead of modifying the existing ones. Referential transparency, idempotence, all the goodies follow.

I'd write (orientative, you get the idea):

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days | .union(names))
  end
end

Notes:

  • I am afraid I will propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add one for each task, and take/return days as symbols? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes or sets, not masks of power of 2 :-)

  • I prefer functional solutions even with OOP, you just return new instances instead of modifying the existing. Referential transparency, idempotence, all the goodies follow.

I'd write:

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days | names)
  end
end

Notes:

  • I am afraid I will propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add one using symbols for the days? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes, trees, or sets, not masks of powers of 2 :-) Here I'd use a set.

  • I prefer functional solutions even with OOP, just return new instances instead of modifying the existing ones. Referential transparency, idempotence, all the goodies follow.

I'd write (orientative, you get the idea):

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days.union(names))
  end
end
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25

Notes:

  • I am afraid I will propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add one for each task, and take/return days as symbols? Add validations if you're worried about invalid values for days.

  • The usual trick of using powers of two as a mask is that, a trick, very handy in low-level languages (C, assembler, ...). In a higher-level language you strive for writing code as declarative as possible, with data structures like lists, hashes or sets, not masks of power of 2 :-)

  • I prefer functional solutions even with OOP, you just return new instances instead of modifying the existing. Referential transparency, idempotence, all the goodies follow.

I'd write:

class WeekSauce
  DAY_NAMES = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  def initialize(days)
    @days = days.to_set & DAY_NAMES
  end
  
  def has_day?(name)
    @days.include?(name)
  end
  
  def add_days(names)
    WeekSauce.new(@days | names)
  end
end