Grasping methods like "to_s"?

Hi, I'm posting here to quickly ask you: how is one able to do something
like:

    "string".is_i?

...where "is_i?" is a method I created, on any object? This kind of goes
into monkey-patching I guess. Would I have to do:

    class String
      # My method
    end

...for every class I want that method to be available upon? So what if I
want a method called "is_i?" to be available for the String, Integer,
and Hash classes? Would I have to add that method to each of the classes
separately (like above)?

Thanks, and I apologise for the, some say, n00b'iness of this question.

Rafal

···

--
Posted via http://www.ruby-forum.com/.

Since all the classes you would want to put it on inherit from Object, you
would define it on Object

class Object
  def is_i?
    false
  end
end

class Integer
  def is_i?
    true
  end
end

"abc".is_i? # => false
123.is_i? # => true

With that said, I'm going to recommend not doing this in any code you
intend to maintain or distribute as a library, because it affects the
environment.

···

On Sat, Mar 30, 2013 at 4:49 PM, Rafal C. <lists@ruby-forum.com> wrote:

Hi, I'm posting here to quickly ask you: how is one able to do something
like:

    "string".is_i?

...where "is_i?" is a method I created, on any object? This kind of goes
into monkey-patching I guess. Would I have to do:

    class String
      # My method
    end

...for every class I want that method to be available upon? So what if I
want a method called "is_i?" to be available for the String, Integer,
and Hash classes? Would I have to add that method to each of the classes
separately (like above)?

Thanks, and I apologise for the, some say, n00b'iness of this question.

OK, thanks both of you.

···

--
Posted via http://www.ruby-forum.com/.

Or perhaps just

class Object
  def is_i?
    self.is_a? Integer
  end
end

Herewith I call the "useless use of self award" (UUOSA) to life. We have a
first nominee. :slight_smile:

SCNR

Cheers

robert

PS: That would have been a good posting for yesterday...

···

On Sat, Mar 30, 2013 at 11:30 PM, Peter Hickman < peterhickman386@googlemail.com> wrote:

Or perhaps just

class Object
  def is_i?
    self.is_a? Integer
  end
end

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Herewith I call the "useless use of self award" (UUOSA) to life. We
have a
first nominee. :slight_smile:

Yeah, is_i? was just an example but I guess it would qualify!

···

--
Posted via http://www.ruby-forum.com/\.

Is this award similar to the "egregious use of cat" in the *nix
command line world?

···

On Tue, Apr 2, 2013 at 5:58 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Sat, Mar 30, 2013 at 11:30 PM, Peter Hickman > <peterhickman386@googlemail.com> wrote:

Or perhaps just

class Object
  def is_i?
    self.is_a? Integer
  end
end

Herewith I call the "useless use of self award" (UUOSA) to life. We have a
first nominee. :slight_smile:

SCNR

Cheers

robert

PS: That would have been a good posting for yesterday...

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Nah, you missed the point of the award.

Cheers

robert

···

On Tue, Apr 2, 2013 at 6:04 PM, Rafal C. <lists@ruby-forum.com> wrote:

> Herewith I call the "useless use of self award" (UUOSA) to life. We
> have a
> first nominee. :slight_smile:

Yeah, is_i? was just an example but I guess it would qualify!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Exactly. See Partmaps.org

Kind regards

robert

···

On Tue, Apr 2, 2013 at 10:00 PM, tamouse mailing lists < tamouse.lists@gmail.com> wrote:

Is this award similar to the "egregious use of cat" in the *nix
command line world?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/