Creating a function in ruby

Posted by Micke on Stack Overflow See other posts from Stack Overflow or by Micke
Published on 2010-04-20T14:10:22Z Indexed on 2010/04/20 14:13 UTC
Read the original article Hit count: 476

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails