Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions source/blog/2016-01-13-opal-0-9-2.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: "Opal 0.9.2: direct JS calls, console.rb, numeric updates, better reflection, and fixes aplenty"
date: 2016/01/13
author: Jared White
---

Opal is now officially on the 0.9.x release path, having just released [0.9.2](https://github.com/opal/opal/tree/v0.9.2). Thanks to all the [contributors to Opal](https://github.com/opal/opal/graphs/contributors?from=2015-04-30&to=2016-01-13&type=c), especially over the holidays, and we're excited for what the new year will bring to the Opal community. Some highlights from the latest release:

### Direct calling of JavaScript methods

You can now make direct JavaScript method calls on native JS objects using the `recv.JS.method` syntax. Has support for method calls, final callback (as a block), property getter and setter (via `#[]` and `#[]=`), splats, JavaScript keywords (via the `::JS` module) and global functions (after `require "js"`).

Some examples, first a simple method call:

```ruby
# javascript: foo.bar()
foo.JS.bar
foo.JS.bar()
```
<!--preview-->
Arguments just like Ruby, with or without parentheses:

```ruby
# JavaScript: foo.bar(1, "a")
foo.JS.bar(1, :a)
foo.JS.bar 1, :a
```

Argument splats (doesn't that Ruby just look great?):

```ruby
# JavaScript: ($a = foo).bar.apply($a, [1].concat([2, 3]))
foo.JS.bar(1, *[2, 3])
foo.JS.bar 1, *[2, 3]
```

Properties work too!

```ruby
# JavaScript: foo["bar"]
foo.JS[:bar]

# JavaScript: fooarr[2]
fooarr.JS[2]
```

That's just the beginning, and should prove to be a great new way to write down-to-JS-level code without interrupting the flow of your Ruby code. [Read the in-depth documentation here](http://opalrb.org/docs/guides/v0.9.2/compiled_ruby.html) (scroll down to the **Calling JavaScript Methods** section).

### Console wrapper added to the stdlib

Requiring the new console wrapper will make available the `$console` global variable. [Check out the docs here](http://opalrb.org/docs/api/v0.9.2/stdlib/Console.html). Example:

```ruby
require 'console'
$console.log a: 1, b: {c: 3}
```

_Note:_ Be advised that `Kernel#pp` no longer forwards arguments directly to `console.log`.

### Updates to Numeric, Complex, and Rational

`Numeric` semantics are now compliant with Ruby, and both `Complex` and `Rational` have been fully implemented.

### Better reflection, exception handling, and method_missing-powered operators

Some updates to Ruby reflection support to allow for deeper metaprogramming abilities. These all are now working: `method_added`, `method_removed`, `method_undefined`, `singleton_method_added`, `singleton_method_removed` and `singleton_method_undefined`.

Improvements to exception handling as well: `Exception#exception`, `Exception::exception`, `Exception#message`, and `Exception#to_s` are fully implemented.

Operator methods (e.g. `+`, `<`, etc.) can be handled by `method_missing`. _Note:_ always be aware of possible performance penalties when not using Opal's optimized operators.

### Bug fixes and more!

As always, Opal 0.9.2 includes many bug fixes and improvements to the internals of the Opal libraries, so be sure to [read the changelog](https://github.com/opal/opal/blob/master/CHANGELOG.md#092-2016-01-10) for further details (as well as information on changes throughout the 0.8.x releases if you didn't see those previously).