# Building expressions

**URL:** <https://www.ruby-forum.com/t/building-expressions/167079>\
**Category:** IronRuby\
**Created:** [May 3, 2009, 12:20pm UTC](https://www.ruby-forum.com/t/building-expressions/167079 "2009-05-03T12:20:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [May 3, 2009, 12:20pm UTC](https://www.ruby-forum.com/t/building-expressions/167079/1 "2009-05-03T12:20:56Z")

</div>

Hi

I’m trying to get some Moq mockery going in IronRuby but Moq expects  
Expression\<Func\<MyObject, TReturn\>\> in his methods.  
Is there a way to make a RubyProc/lambda return an expression or convert  
it  
to an expression?

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

Steve M.  
[http://www.brainyquote.com/quotes/authors/s/steve\_martin.html](http://www.brainyquote.com/quotes/authors/s/steve_martin.html)

- “I’ve got to keep breathing. It’ll be my worst business mistake if I  
don’t.”

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [May 3, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/building-expressions/167079/2 "2009-05-03T23:37:57Z")

</div>

It cannot be done. Even if you could get to the ExpressionTree of a Ruby  
lambda, the ExpressionTree would be loosely-bound as Ruby uses  
duck-typing, whereas Moq expects a strongly-typed ExpressionTree so that  
it can figure out the exact method that you are trying to associate some  
expectation with.

FWIW, Mark had also been playing with mocking, and in our discussions,  
we said that the easiest mocking approach would be to use Ruby features  
like method\_missing, monkey-patching, etc (like rspec does). Ie.  
something like this:

def mock(clrInterface):  
class ClrMock \< clrInterface  
# monkey-patch all of â€œclrInterface.methods" here, and redirect to  
the mocking framework  
end  
return ClrMock.new  
end

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ivan Porto  
Carrero  
Sent: Sunday, May 03, 2009 3:18 AM  
To: ironruby-core  
Subject: [Ironruby-core] Building expressions

Hi

I’m trying to get some Moq mockery going in IronRuby but Moq expects  
Expression\<Func\<MyObject, TReturn\>\> in his methods.  
Is there a way to make a RubyProc/lambda return an expression or convert  
it to an expression?

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

Steve  
Martin[http://www.brainyquote.com/quotes/authors/s/steve\_martin.html](http://www.brainyquote.com/quotes/authors/s/steve_martin.html) -  
“I’ve got to keep breathing. It’ll be my worst business mistake if I  
don’t.”

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [May 5, 2009, 4:26pm UTC](https://www.ruby-forum.com/t/building-expressions/167079/3 "2009-05-05T16:26:00Z")

</div>

Ivan,

You may want to try Jeremy Mcanally’s  
stump[http://github.com/jeremymcanally/stump/tree/master](http://github.com/jeremymcanally/stump/tree/master)mocking  
framework. I haven’t used it much, so I can’t give any additional  
info on it. It may not be what you are wanting, but if you just need  
mocks,  
it may be worth a shot.

Ryan R.  
removed\_email\_address@domain.invalid  
[http://panesofglass.org/](http://panesofglass.org/)  
[http://wizardsofsmart.net/](http://wizardsofsmart.net/)

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [May 5, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/building-expressions/167079/4 "2009-05-05T16:47:11Z")

</div>

Yes  
I have since changed my approach. I’ve also talked with Mark about  
approaching this.

The approach I take now is to generate a proxy to the  
object/class/interface  
that will record method calls.  
After recording you can then run verifications on those method calls.  
The  
way I generate the proxy for an interface came out of my talk with Mark.

> <https://github.com/casualjim/caricature/blob/922bdcd11d9b0a1aa5500c3fe430483863d9041e/lib/caricature/proxy.rb#L92>

But the deeper implication is then that when a library uses  
Expression\<Func\<,\>\> as parameters you cannot use it from ironruby at  
all?  
Not even by building the expression tree yourself?

## (I forgot to hit send apparently :))

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
GSM: +32.486.787.582  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

Emo Philips  
[http://www.brainyquote.com/quotes/authors/e/emo\_philips.html](http://www.brainyquote.com/quotes/authors/e/emo_philips.html)

- “I got some new underwear the other day. Well, new to me.”

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [May 5, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/building-expressions/167079/5 "2009-05-05T17:11:22Z")

</div>

Of course you should be able to build the expression tree manually â€“  
though even here, you may have trouble under .NET 3.5 as a result of the  
duplicated namespace. (That issue goes away with .NET 4.)

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ivan Porto  
Carrero  
Sent: Tuesday, May 05, 2009 7:46 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Building expressions

Yes  
I have since changed my approach. I’ve also talked with Mark about  
approaching this.

The approach I take now is to generate a proxy to the  
object/class/interface that will record method calls.  
After recording you can then run verifications on those method calls.  
The way I generate the proxy for an interface came out of my talk with  
Mark.

> <https://github.com/casualjim/caricature/blob/922bdcd11d9b0a1aa5500c3fe430483863d9041e/lib/caricature/proxy.rb#L92>

But the deeper implication is then that when a library uses  
Expression\<Func\<,\>\> as parameters you cannot use it from ironruby at  
all? Not even by building the expression tree yourself?

## (I forgot to hit send apparently :))

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
GSM: +32.486.787.582  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

Emo  
Philips[http://www.brainyquote.com/quotes/authors/e/emo\_philips.html](http://www.brainyquote.com/quotes/authors/e/emo_philips.html) -  
“I got some new underwear the other day. Well, new to me.”  
On Sun, May 3, 2009 at 11:37 PM, Shri B.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

It cannot be done. Even if you could get to the ExpressionTree of a Ruby  
lambda, the ExpressionTree would be loosely-bound as Ruby uses  
duck-typing, whereas Moq expects a strongly-typed ExpressionTree so that  
it can figure out the exact method that you are trying to associate some  
expectation with.

FWIW, Mark had also been playing with mocking, and in our discussions,  
we said that the easiest mocking approach would be to use Ruby features  
like method\_missing, monkey-patching, etc (like rspec does). Ie.  
something like this:

def mock(clrInterface):

class ClrMock \< clrInterface

```
# monkey-patch all of â€œclrInterface.methods" here, and redirect to 

```

the mocking framework

end

return ClrMock.new

end

From:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Ivan Porto C.  
Sent: Sunday, May 03, 2009 3:18 AM  
To: ironruby-core  
Subject: [Ironruby-core] Building expressions

Hi

I’m trying to get some Moq mockery going in IronRuby but Moq expects  
Expression\<Func\<MyObject, TReturn\>\> in his methods.  
Is there a way to make a RubyProc/lambda return an expression or convert  
it to an expression?

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

Steve  
Martin[http://www.brainyquote.com/quotes/authors/s/steve\_martin.html](http://www.brainyquote.com/quotes/authors/s/steve_martin.html) -  
“I’ve got to keep breathing. It’ll be my worst business mistake if I  
don’t.”

* * *

Ironruby-core mailing list  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)
