Problem with calling ruby procs from javascript

23 views
Skip to first unread message

Aaron Renner

unread,
Apr 11, 2012, 3:43:47 PM4/11/12
to therub...@googlegroups.com
I am having a problem with the following code:

ctx = V8::Context.new

ctx[:sample]=lambda {|name| "Hello #{name}"}

ctx.eval("sample('world')")

=> "Hello [object global]"


Could someone shed some light on why it's not returning "Hello world"?

Thanks,
Aaron 

Charles Lowell

unread,
Apr 11, 2012, 3:46:40 PM4/11/12
to The Ruby Racer
Yes, the first argument to your lambda is always the JavaScript `this`
object, whatever that happens to be. In this case, it is the context's
global object. What you want is:

ctx[:sample]=lambda {|this, name| "Hello #{name}"}

>
> Thanks,
> Aaron

Aaron Renner

unread,
Apr 11, 2012, 6:15:43 PM4/11/12
to therub...@googlegroups.com
Looks like this could be a problem with version 0.10.1. It works correctly in 0.9.8. I'll open a bug report.

Aaron

Aaron Renner

unread,
Apr 11, 2012, 7:18:51 PM4/11/12
to therub...@googlegroups.com
Ok, thanks. Sorry about the bug report. I didn't see your reply.


On Wednesday, April 11, 2012 1:46:40 PM UTC-6, Charles Lowell wrote:
Reply all
Reply to author
Forward
0 new messages