Defining #blank for Array.

75 views
Skip to first unread message

Michael Boutros

unread,
Jul 9, 2012, 10:15:04 AM7/9/12
to rubyonra...@googlegroups.com
Hello:

1.9.3p194 :014 > "".blank?
 => true 
1.9.3p194 :015 > ["", ""].blank?
 => false 

Proposal: the second line should also produce true.

Thoughts?

Geoff Harcourt

unread,
Jul 9, 2012, 10:18:04 AM7/9/12
to rubyonra...@googlegroups.com
Michael,

That array isn't blank, empty strings aren't the same as nils. I think changing that behavior would be confusing. If you don't want to count those elements, it's probably best to filter the array for non-blank strings.

-Geoff


-- 
Geoff Harcourt

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/Rbgmrz1cT7AJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.

Matt Jones

unread,
Jul 9, 2012, 10:22:30 AM7/9/12
to rubyonra...@googlegroups.com
If you really want to check for "are all the values in this array blank", try:

some_array.all?(&:blank?)

--Matt Jones

Michael Boutros

unread,
Jul 9, 2012, 10:23:03 AM7/9/12
to rubyonra...@googlegroups.com
Geoff,

But from the Rails documentation: An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are all blank. That's why I think an array full of false, empty, or whitespace strings should be empty.

- Michael

On Monday, July 9, 2012 10:18:04 AM UTC-4, Geoff Harcourt wrote:
Michael,

That array isn't blank, empty strings aren't the same as nils. I think changing that behavior would be confusing. If you don't want to count those elements, it's probably best to filter the array for non-blank strings.

-Geoff


-- 
Geoff Harcourt

On Monday, July 9, 2012 at 10:15 AM, Michael Boutros wrote:

Hello:

1.9.3p194 :014 > "".blank?
 => true 
1.9.3p194 :015 > ["", ""].blank?
 => false 

Proposal: the second line should also produce true.

Thoughts?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/Rbgmrz1cT7AJ.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com.

Xavier Noria

unread,
Jul 9, 2012, 10:25:00 AM7/9/12
to rubyonra...@googlegroups.com
On Mon, Jul 9, 2012 at 4:23 PM, Michael Boutros <michael...@gmail.com> wrote:
Geoff,

But from the Rails documentation: An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are all blank. That's why I think an array full of false, empty, or whitespace strings should be empty.

Why? [""] is not an empty array.

Maybe this description may help?

Jarrett Meyer

unread,
Jul 9, 2012, 10:26:33 AM7/9/12
to rubyonra...@googlegroups.com
An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are > all blank. That's why I think an array full of false, empty, or whitespace strings should be empty.

But that's not mathematically accurate.

"".size #=> 0
[""].size #=> 1
[nil].size #=> 1

An array with a single item is no longer empty, even if the item itself is empty.

--
Jarrett Meyer
Email: jarret...@gmail.com
Web: JarrettMeyer.com
Twitter: @jarrettmeyer



To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/KDc5odVcWngJ.

To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.

Michael Boutros

unread,
Jul 9, 2012, 10:30:04 AM7/9/12
to rubyonra...@googlegroups.com
Sorry, where I last wrote "empty" I meant to write "blank," ie: That's why I think an array full of false, empty, or whitespace strings should be _blank_. Rails defines blank as a convenience method instead of checking if something is nil and then if it's empty or not. I think extending that convenience to arrays makes sense. To clarify:

["", ""].empty? # false
["", ""].blank? # true

- Michael

Michael Boutros

unread,
Jul 9, 2012, 10:31:26 AM7/9/12
to rubyonra...@googlegroups.com
https://gist.github.com/3076887


On Monday, July 9, 2012 10:15:04 AM UTC-4, Michael Boutros wrote:

Xavier Noria

unread,
Jul 9, 2012, 10:31:30 AM7/9/12
to rubyonra...@googlegroups.com
Ah, OK. So the definition of blank? is clear but you are proposing to change it for Arrays right?

Michael Boutros

unread,
Jul 9, 2012, 10:38:33 AM7/9/12
to rubyonra...@googlegroups.com
Yes.

Xavier Noria

unread,
Jul 9, 2012, 10:50:34 AM7/9/12
to rubyonra...@googlegroups.com
On Mon, Jul 9, 2012 at 4:38 PM, Michael Boutros <michael...@gmail.com> wrote:

Yes.

Your implementation makes sense. I mean, I believe that if blank? was defined on enumerables as all?(&:blank?) from the very first day, one could have accepted that definition just fine.

But the current definition also makes sense to me.

Since this is a fundamental predicate in Active Support that has had this semantic since forever, this modification would be backwards incompatible, and the current definition is also just fine in my opinion, I think we should keep it the way it is.

Scott Gonyea

unread,
Jul 9, 2012, 1:21:52 PM7/9/12
to Ruby on Rails: Core
Lock down your data at the gates and this is not a problem. Making
#blank? incredibly slow is silly.

Maurício Linhares

unread,
Jul 9, 2012, 10:17:33 AM7/9/12
to rubyonra...@googlegroups.com
The array has objects on it, so it isn't blank.


-
Maurício Linhares
http://techbot.me/ - http://twitter.com/#!/mauriciojr
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/Rbgmrz1cT7AJ.
> To post to this group, send email to rubyonra...@googlegroups.com (mailto:rubyonra...@googlegroups.com).
> To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com (mailto:rubyonrails-co...@googlegroups.com).

Oscar Del Ben

unread,
Jul 9, 2012, 10:39:13 AM7/9/12
to rubyonra...@googlegroups.com
That's very unlikely to happen. Pretty much all Rails app rely on this behavior.

-- 
Oscar Del Ben
Sent with Sparrow

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/st6ddnnWkPMJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.

Oscar Del Ben

unread,
Jul 9, 2012, 10:24:07 AM7/9/12
to rubyonra...@googlegroups.com
Empty means that it has no elements.

-- 
Oscar Del Ben
Sent with Sparrow

To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/KDc5odVcWngJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.

Scott Gonyea

unread,
Jul 9, 2012, 12:41:05 PM7/9/12
to rubyonra...@googlegroups.com
Yes, we need #blank? to be extra expensive because beginners needn't control what gets injected in their Arrays.  Maybe `{"" => ""}.blank?` should be true, too?

Here you go:

class Array
  def super_blank?
    # Go!
  end
end

Scott

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/Rbgmrz1cT7AJ.

Oscar Del Ben

unread,
Jul 9, 2012, 10:31:55 AM7/9/12
to rubyonra...@googlegroups.com
Blank is a convenience method for not checking the type of object. If you want to do that you should use:

array.all?(&:blank)

-- 
Oscar Del Ben
Sent with Sparrow

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/6QBZ6XsNQU0J.

Michael Boutros

unread,
Jul 10, 2012, 2:55:37 PM7/10/12
to rubyonra...@googlegroups.com
Do you think anyone currently uses #blank? on enumerables in its current state? It doesn't make sense the way it is right now. Imagine a piece of paper with a list of 10 blank lines. Wouldn't you call that a blank list?

James Coleman

unread,
Jul 10, 2012, 2:58:02 PM7/10/12
to rubyonra...@googlegroups.com
I don't think that blank? makes sense on anything other than String, no matter how you define it. My vote would be to deprecate it on Enumerable, for example, and add something else if desired.

James Coleman

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/WL4-tEq6TT0J.

Andrés Mejía

unread,
Jul 10, 2012, 2:58:35 PM7/10/12
to rubyonra...@googlegroups.com
I use it. It makes total sense to me.

If you have a piece of paper with a list of 10 blank lines, how do you know there are exactly 10 blank lines instead of 3 or 7 or 13?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.

Rafael Mendonça França

unread,
Jul 10, 2012, 2:58:28 PM7/10/12
to rubyonra...@googlegroups.com
Yes, I think everyone uses #blank? on enumerables in its current state, and expect that behavior, because it is well documented.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
Reply all
Reply to author
Forward
0 new messages