I''ve heard this question asked before but I couldn''t find the appropriate responces that I was looking for, but what is the difference between Object.nil? and Object.blank? Is it how one handles white and newline characters? To me they almost seem the same but I imagine there has to be some differences. Thanks, -S -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
An object is blank if it''s nil, empty, or a whitespace string. If it''s nil, it''s nil. On 10/9/07, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''ve heard this question asked before but I couldn''t find the > appropriate responces that I was looking for, but what is the difference > between Object.nil? and Object.blank? Is it how one handles white and > newline characters? To me they almost seem the same but I imagine there > has to be some differences. Thanks, > > -S > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 9, 2007, at 4:03 PM, Shandy Nantz wrote:> I''ve heard this question asked before but I couldn''t find the > appropriate responces that I was looking for, but what is the > difference > between Object.nil? and Object.blank? Is it how one handles white and > newline characters? To me they almost seem the same but I imagine > there > has to be some differences. Thanks, > > -SUse the Source... (vendor/rails/activesupport/lib/active_support/core_ext/blank.rb) class Object #:nodoc: # "", " ", nil, [], and {} are blank def blank? if respond_to?(:empty?) && respond_to?(:strip) empty? or strip.empty? elsif respond_to?(:empty?) empty? else !self end end end class NilClass #:nodoc: def blank? true end end class FalseClass #:nodoc: def blank? true end end class TrueClass #:nodoc: def blank? false end end class Array #:nodoc: alias_method :blank?, :empty? end class Hash #:nodoc: alias_method :blank?, :empty? end class String #:nodoc: def blank? empty? || strip.empty? end end class Numeric #:nodoc: def blank? false end end -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org +1 513-295-4739 Skype: rob.biedenharn --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
blank is: "" " " nil [] {} false any of the above applied with .blank? will return true. "".blank? # returns true [].blank? # returns true {""=>""}.blank? # returns false {}.blank? # returns true -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks all, that helps out a lot. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Use the Source... > (vendor/rails/activesupport/lib/active_support/core_ext/blank.rb)I''d like to have a .blank? for Time and date where 0000-00-00 00:00:00 etc is considered blank. Where''s the right place w/in a Rails project to add an extension to Ruby''s classes to accomplish that? -- gw -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 3, 2007, at 1:22 AM, Greg Willits wrote:>> Use the Source... >> (vendor/rails/activesupport/lib/active_support/core_ext/blank.rb) > > I''d like to have a .blank? for Time and date where 0000-00-00 00:00:00 > etc is considered blank. > > Where''s the right place w/in a Rails project to add an extension to > Ruby''s classes to accomplish that? > > -- gwWell, for my projects, I''ve started putting these kinds of things into a file in: RAILS_ROOT/lib/ext/some_class_or_behavior_name.rb and I require them in my config/environment.rb like this: Dir[File.join(RAILS_ROOT, ''lib'', ''ext'', ''*.rb'')].each do |f| base = File.basename(f, ''.rb'') require "ext/#{base}" end One project, for example, has files: active_record_compare.rb array.rb enumerable.rb file.rb integer.rb nil_class.rb range.rb string.rb web_agent.rb And my nil_class.rb contains: class NilClass # Allowing a chain like: value.nonblank? || ''default value'' def nonblank? self end # so it plays nicely with Numeric#nonzero? def nonzero? self end end That should give you some ideas. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 3, 2007, at 5:35 AM, Rob Biedenharn wrote:> On Nov 3, 2007, at 1:22 AM, Greg Willits wrote: >>> Use the Source... >>> (vendor/rails/activesupport/lib/active_support/core_ext/blank.rb) >> >> I''d like to have a .blank? for Time and date where 0000-00-00 >> 00:00:00 >> etc is considered blank. >> >> Where''s the right place w/in a Rails project to add an extension to >> Ruby''s classes to accomplish that? > > > Well, for my projects, I''ve started putting these kinds of things into > a file in: > RAILS_ROOT/lib/ext/some_class_or_behavior_name.rb > and I require them in my config/environment.rb like this:Makes sense. Thanks. -- gw --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---