# Selective runtime provision of instance methods

**URL:** <https://www.ruby-forum.com/t/selective-runtime-provision-of-instance-methods/55217>\
**Category:** Ruby\
**Created:** [March 17, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/selective-runtime-provision-of-instance-methods/55217 "2006-03-17T23:46:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julian\_ISKamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@Julian\_ISKamil](https://www.ruby-forum.com/u/Julian_ISKamil)\
**Post date:** [March 17, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/selective-runtime-provision-of-instance-methods/55217/1 "2006-03-17T23:46:20Z")

</div>

I’d like to get some comments and suggestions from anyone who may have  
an opinion on this or know of a better way to accomplish this.

I have an object with a lot of methods and variables some of which (but  
not all) I’d like to make callable from another object that happens to  
be evaluated by ERB. So I did this:

`
class LotsOfMethods
    # to be made callable
    def method1 ... end
    def method2 ... end
    ...
```
# not callable
def methodX ... end

def delegate
    d = OpenStruct.new({
        :var1 => var1,
        :var2 => var2,
        ...
        })

    my = self
    d_singleton = class << d; self end
    d_singleton.send(:define_method, :method1) { my.method1 }
    d_singleton.send(:define_method, :method2) { my.method2 }
    ...

    d
end

```
end
class Sandbox
def self.env(delegate)
Proc.new {}
end
end
`

`sacred_object = LotsOfMethods.new
output = ERB.new(text).result(Sandbox.env(sacred_object.delegate))
`

The idea is that the embedded Ruby code in “text” will be able to  
access the specified variables and methods in ‘delegate’ but not any  
others. So, for example:

text = ‘\<%= delegate.method1 %\>’

will be allowed; but:

text = ‘\<%= delegate.methodX %\>’

will raise an exception.

Does anyone know of a better way to accomplish the same effect?

Best regards,

Julian I. Kamil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://pandora.rubyveil.com/](http://pandora.rubyveil.com/) - Pandora Publisher  
[http://books.rubyveil.com/](http://books.rubyveil.com/) - The Ruby Bookshelf
