Skip to content
Permalink
Browse files

update docs to remove ruby 1.9 details

  • Loading branch information...
saturnflyer committed Jul 28, 2016
1 parent 82d25ea commit 116dbacf4fa55de32d9cae15c9766c934fe65502
Showing with 4 additions and 12 deletions.
  1. +4 −12 README.md
@@ -18,7 +18,7 @@ Here's a quick example that you might try in a Rails project:
# implement a module that contains information for the request response
# and apply it to an object in your system.
def show
respond_with user.cast_as(UserRepresenter)
@user = user.cast_as(UserRepresenter)
end
```

@@ -293,22 +293,14 @@ Person.instance_method(:hello) #=> #<UnboundMethod: Person#hello>
But if you attempt to use that `UnboundMethod` on an object that is not a `Person` you'll get
an error about a type mismatch.

Casting will bind an unbound method to a client object and execute the method as though it is
Casting will bind an UnboundMethod method to a client object and execute the method as though it is
defined on the client object. Any reference to `self` from the method block will refer to the
client object.

This behavior is different in Ruby 1.9 vs. 2.x.

According to [http://rubyspec.org](http://rubyspec.org) the behavior in MRI in 1.9 that allows this to happen is incorrect. In MRI (and JRuby) 1.9 you may unbind methods from an object that has been extended with a module, and bind them to another object of the same type that has *not* been extended with that module.

Casting uses this as a way to trick the interpreter into using the method where we want it and avoid forever extending the object of concern.

This changed in Ruby 2.0 and does not work. What does work (and is so much better) in 2.0 is that you may take any method from a module and apply it to any object. This means that Casting doesn't have to perform any tricks to temporarily apply behavior to an object.

For example, this fails in 1.9, but works in 2.0:
Rather than define methods on classes, you may take any method from a module and apply it to any object regardless of its class.

```ruby
GreetingModule.instance_method(:hello_world).bind(actor).call
GreetingModule.instance_method(:hello).bind(actor).call
```

Casting provides a convenience for doing this.

0 comments on commit 116dbac

Please sign in to comment.
You can’t perform that action at this time.