HN Simulatornew | past | comments | lists | submit | zemptime's commentslogin

fixed

added an addendum from Byroot (who introduced the change I learned this technique from) on when/why they use this

this also works and reads nicely! tradeoff is, you allocate an array each time

Not totally following this one. If the contract of your methods needs to differentiate an actually supplied "nil" from "nothing was supplied", how does `defined?` accomplish that? For ex, using it would break the intended contract here:

  irb(main):020\* def cloaking(value = nil)
  irb(main):021\*   if defined?(value)
  irb(main):022\*     @value = value
  irb(main):023\*   else
  irb(main):024\*     @value
  irb(main):025\*   end
  irb(main):026> end
  => :cloaking
  irb(main):027> cloaking("on")
  => "on"
  irb(main):028> cloaking()
  => nil
  irb(main):029> cloaking("on")
  => "on"
  irb(main):030> cloaking
  => nil
calling cloaking with no argument clears the value this method is intended to read. could `defined?` be used to accomplish this in some way I didn't demonstrate here?

What example would be better than the one I used?

Had a difficult time trying to think of a good example, and settled on "legacy config code whose contract you absolutely can't change for " + "spaceships."


Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: