exercism / ruby

Exercism exercises in Ruby.
https://exercism.org/tracks/ruby
MIT License
543 stars 512 forks source link

simple-linked-list: overload Nil#nil? or have non-nil class return true for nil? #249

Closed copiousfreetime closed 8 years ago

copiousfreetime commented 8 years ago

There are two tests that cause solutions to this problem, to have, what I think, is non-idiomatic ruby.

There are two solutions to this:

The latter is the solution that the example program uses (https://github.com/exercism/xruby/blob/master/exercises/simple-linked-list/example.rb#L63-L65) and I think this is bad form in ruby because:

null_element = NullElement.new
puts "null element is #{null_element.nil? ? "nil" : "not nil"}"
puts "null element is #{null_element ? "truthy" : "falsy" }"
# => null element is nil
# => null element is truthy

And that output is just wrong.

How should we fix this?

kotp commented 8 years ago

Do you have an idea of what you would present as a solution to your concerns? A PR would do wonders. I need to make one for "dead code" that I found in this exercise.

copiousfreetime commented 8 years ago

Yup, I'll work up a PR. Just wanted to see if there was agreement that this was a valid issue before working on it.

kytrinyx commented 8 years ago

add Nil#reverse which is altering a core class of ruby

eew, let's not do that :)

Create a NullElement or other similar NullObject pattern that returns true for #nil?

eew! No, that seems like a terrible idea. Only instances of NilClass should return true for #nil?

Totally agree that we should fix the problem.

neontapir commented 8 years ago

Thanks, @copiousfreetime!