Method: JS::Object#method_missing
- Defined in:
- lib/js.rb
#method_missing(sym, *args, &block) ⇒ Object
Provide a shorthand form for JS::Object#call
This method basically calls the JavaScript method with the same name as the Ruby method name as is using JS::Object#call.
Exceptions are the following cases:
-
If the method name ends with a question mark (?), the question mark is removed and the method is called as a predicate method. The return value is converted to a Ruby boolean value automatically.
This shorthand is unavailable for the following cases and you need to use JS::Object#call instead:
-
If the method name is invalid as a Ruby method name (e.g. contains a hyphen, reserved word, etc.)
-
If the method name is already defined as a Ruby method under JS::Object
-
If the JavaScript method name ends with a question mark (?)
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/js.rb', line 184 def method_missing(sym, *args, &block) sym_str = sym.to_s if sym_str.end_with?("?") # When a JS method is called with a ? suffix, it is treated as a predicate method, # and the return value is converted to a Ruby boolean value automatically. result = invoke_js_method(sym_str[0..-2].to_sym, *args, &block) # Type coerce the result to boolean type # to match the true/false determination in JavaScript's if statement. return ::JS.global.Boolean(result) == ::JS::True end invoke_js_method(sym, *args, &block) end |