2

I want to check the type of strings that I encounter:

class String
    def is_i?
        /\A[-+]?\d+\z/ === self
    end

    def is_path?
        pn = Pathname.new(self)
        pn.directory?
    end     
end

def check(key)
    puts case key
    when is_i?
        puts "Could be a number"
    when is_path?
        puts "This is a path"
    else
        puts "Ok"
    end
end

When I run check("1345425") I get the following error:

undefined method `is_i?' for main:Object (NoMethodError)

What should I do to correct it?

2
  • 1
    Your check method is not defined on the String class but you're calling is_i? on it. Try writing when key.is_i? and when key.is_path? instead. Commented Oct 27, 2015 at 16:53
  • 1
    The methods you defined on string are fine. Your problem is you've misunderstood how case works. Commented Oct 27, 2015 at 16:58

3 Answers 3

2

You have defined functions on String instance, hence:

def check(key)
    puts case
         when key.is_i?
           "Could be a number"
         when key.is_path?
           "This is a path"
         else
           "Ok"
         end
end

or

def check(key)
    puts case key
         when ->(s) { s.is_i? }
           "Could be a number"
         when ->(s) { s.is_path? }
           "This is a path"
         else
           "Ok"
         end
end

UPD Please also note that I removed superfluous subsequent calls to puts.

Sign up to request clarification or add additional context in comments.

Comments

1

Your check method isn't defined on the String class, so when you write when is_i? and when is_path? it is trying to call those methods on your main, hence the error.

You should call these methods on your String object (key) like this:

...
when key.is_i?
...
when key.is_path?
...

Comments

0

You may change it like this

class String
  def is_i?
    /\A[-+]?\d+\z/ === self
  end

  def is_path?
    pn = Pathname.new(self)
    pn.directory?
  end
end

def check(hash_key)
  puts case hash_key
  when Proc.new { hash_key.is_i? }
    return "Could be a number"
  when Proc.new { hash_key.is_path? }
    return "This is a path"
  else
    return "Ok"
  end
end

Running check('1312312') in irb returns result as "Could be a number"

1 Comment

Please never use return in ruby like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.