Question: I want to create a function that messes around with the classes passed to it. What would be the most idiomatic way to reopen those classes to add functionality? Here's what I mean:

```
def class_messer(target_object)
  #would like to reopen class here with something like:
  class target_object.class
    #add methods
  end
end

```

Obviously that syntax doesn't work. I could get the target\_object's class and eval some strings, but that feels gross. Is there a more idiomatic way to do this?

Answer: 
I think you're looking for `class_eval`. If you want to reopen a class and you do not have the constant as is, but a reference, you can call `class_eval` on it and pass a block (or even a string) of code to be evaluated in that classes context.

```
def class_messer(target_object)

  # assuming that target_object is an instance of desired class

  target_object.class.class_eval do
    #add methods
  end

end

```
Maybe it's not correct to change class, for example, if you had an instance of Array class and changed its class, then this change could impact on other instances of Array class. So instead use [singleton class](https://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby) of instance and the definition of method will be:

```
target_object.send(:define_method, :new_method) do
  #... 
end

```

or

```
class << target_object
  def new_method
    #...
  end
end

```