From 7b85d0d6cf8369690d90071976c43d02ab6e095a Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Fri, 3 Jan 2014 12:02:04 +0200 Subject: [PATCH] add additional examples of accessing JavaScript values --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4a47102..baceb02 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,20 @@ evaluate some simple JavaScript cxt = V8::Context.new cxt.eval('7 * 6') #=> 42 -call JavaScript functions +access values inside your JavaScript context from Ruby + + cxt.eval 'var val = {num: 5, fun: function isTruthy(arg) { return !!arg }}' + val = cxt[:val] #=> V8::Object + cxt[:val] == cxt.scope.val #=> true + val.num #=> 5 + val.isTruthy(1) #=> true + +this includes references to JavaScript functions + + truthy = val[:isTruthy] #=> V8::Function + truthy.call(' ') #=> true + truthy.call(0) #=> false - cxt.eval "function isTruthy(arg) { return !!arg }" - cxt[:isTruthy].call(' ') #=> true - cxt.scope.isTruthy(0) #=> false embed values into the scope of your context