Creating a function in ruby
- by Micke
Hello, i have some problem with creating a function to my Rails app.
I want it to work like this:
str = "string here"
if str.within_range?(3..30)
puts "It's withing the range!"
end
To do that i added this into my application helper:
def within_range?(range)
if self.is_a?(String)
(range).include?(self.size)
elsif self.is_a?(Integer)
(range).include?(self)
end
end
But i get this error:
undefined method `within_range?' for "":String
Do you know what the problem is? and could you please help me.
If there is a easier way please say so then.
Thanks in advance, Micke.