I want to define some global functions, I mean, some functions that I
can use from controllers or views, in example
def empty_string?(cadena)
if (cadena == nil) || (cadenal.length == 0)
true
else
false
end
end
where can I put this function? what if I have manny functions like this?
Thank you
Put them in a helper and include them in your controller(s).
If you want them to be available everywhere, write a new helper, say
GlobalHelper in a file called global_helper and in your
application_helper and application_controller say
class Application[Helper|Controller]
include GlobalHelper
…
end
BTW, a better way to implement this function is:
def empty_string? s
!(s && !s.empty?)
end
Better still, you can add an empty? method to NilClass, which negates
the need for the function in the first place:
class NilClass
def empty?
true
end
end
This means that you can now call empty? on a nil object or a string,
nil always returning true.
If you have many functions like that, you should really spend some
time reading the pickaxe book and the Ruby api docs. Also have a look
at the ruby facets library. I think you’ll find that a lot of what you
are trying to do is either already there or can be implemented in a
better way.
Max