Only-Except, not_nil?

Not sure what you are talking about on number 1. For number 2 use

Object#blank?

And

Object#present?

  1. Something like:

  • class Array

  • def filter_by_only_except options

  • self & Array.wrap(options[:only] || self) - Array.wrap(options[:except] || [])

  • end

  • end

  1. No, present? is (not blank?) but not (not nil?)

Good example! blank? has an opposite method but not nil?. Yes?

Use a 3rd party lib like http://extensions.rubyforge.org/rdoc/index.html. It has non_nil?, not_nil?

blank? and present? probably satisfy most rails devs needs.

Sure I can write my own one-line not_nil? method :slight_smile: But is it logical to:

  1. have nil?
  2. have present? in opposite to blank?
  3. don’t have not_nil?
    Or can’t I say about logical here?

Sure I can write my own one-line not_nil? method :slight_smile: But is it logical to:

  1. have nil?
  2. have present? in opposite to blank?
  3. don’t have not_nil?
    Or can’t I say about logical here?

The standard Ruby idiom for the inverse of if object.nil? is if object. Yes, there is an edge case when object == false, but other than that it works great and should be how you write your code. There is no need for another method that just makes code wordier and harder to read.

–josh

“How many ruby developers does it does to ask if something is not false?”

Although non-standard, I would argue that the most logical way to write that is:

unless object.nil?

I know it’s a double-negative, but in fact if you think about it it makes perfect sense – you’re saying you want to do something unless this thing is nil.

And while the standard may be to say if object, you still have to take a mental moment when looking at that code to think about truthy and falsy, whereas my proposal above basically means you don’t have to take that split second to consider truthy and falsy and you get around Josh’s edge case too.

It seems to me that if what you’re interested in is a thing being not nil, you should write code that expresses that, instead of writing code where the next developer may have to think about the many truthy values of ruby.

Obviously, this is a 6-of-one-half-dozen-of-the-other question.

Another double-negative idiom for truthiness is "!!":
  !!ob ? something_main_and_important : 'no'

I can't see much sense in an additional ".non_nil?" method when
there's at least three alternatives already given :slight_smile: