has anyone done something like this?
class NilClass
def empty?; true; end
def strip; nil; end
def ; nil; end
end
which is nice so that you can do this without throwing exception:
if !params[:something].empty?
if !params[:something][:else].strip.empty?
as opposed to this:
if params[:something] && !params[:something].empty?
if params[:something] && params[:something][:else] && !
params[:something][:else].strip.empty?
It seems to work fine for me so far. I’m just wondering what’s the
negative effect of doing that? If not then shouldn’t we be doing
this
?
Reynard wrote:
has anyone done something like this?
class NilClass
def empty?; true; end
def strip; nil; end
def ; nil; end
end
which is nice so that you can do this without throwing exception:
if !params[:something].empty?
if !params[:something][:else].strip.empty?
as opposed to this:
if params[:something] && !params[:something].empty?
if params[:something] && params[:something][:else] && !
params[:something][:else].strip.empty?
It seems to work fine for me so far. I’m just wondering what’s the
negative effect of doing that? If not then shouldn’t we be doing
this
?
You disable errors that make perfect sense. The problem is that you
really get into a world of pain if you do not expect any nil values is
you Hash (or the expected Hash to be nil instead). This is when you want
to see those errors.
I see your problem with if params[:something][:else], but your first
example is already solved by such a technique: Rails has #blank? which
is defined on nil.
if params[:something] && !params[:something].empty?
=>
unless params[:something].blank?
I agree,
blank? is the way.
otherwise I suggest;
class NilClass
def method_missing(method_name, *args)
return true
end
end
Florian G. wrote:
Reynard wrote:
has anyone done something like this?
class NilClass
def empty?; true; end
def strip; nil; end
def ; nil; end
end
ah I knew there must be a better way. It’s strange that I cannot find
blank? in the ruby or rails api documentation.
Thanks a lot,
On Jul 19, 5:04 am, Matthew R. [email protected]