Skip to content

Commit 7f758d6

Browse files
committed
Describe limitations and add example use case
1 parent fc9f87b commit 7f758d6

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ And then execute:
3232
```ruby
3333
{
3434
speed: function(val) {
35-
return parseFloat(val).toLocaleString();
35+
return '$' + parseFloat(val).toLocaleString();
3636
}
3737
}.to_json
38-
#=> "{\"speed\":function(val) { return parseFloat(val).toLocaleString(); }}"
38+
#=> "{\"speed\":function(val) { return '$' + parseFloat(val).toLocaleString(); }}"
3939
```
4040

41-
But, don't go wild just yet and try not to put multiple functions on the same
42-
line.
41+
But, don't go wild just yet because there are limitations:
42+
- You cannot use `if` keyword because the way JavaScript uses it is different from
43+
ruby. It is meant to be used for a simple function. One use case I'm targetting
44+
is for formatting options to be used by JavaScript libraries.
45+
- Also, don't put multiple functions on the same line. You'll get unexpected result
46+
if you do.
47+
- You should end statement with `;`.
4348

4449
If you try the above example on your REPL (e.g. `irb` or `pry`) it won't
4550
work since the gem will try to find the file that stores the code. So, you
@@ -49,7 +54,7 @@ need to put it on a file and run it.
4954

5055
Well, technically, it's not a JavaScript function. It's a Ruby method named
5156
`function` that's available in any object. In JavaScript, a form like that is
52-
a _function declaration_, but in Ruby it's a method invocation. So, in that case,
57+
a _function declaration_, but in Ruby it's a _method invocation_. So, in that case,
5358
`val` needs to already exist before you call it, and it is. Both `function` and
5459
`val` are already a method of an object. If you want to use arguments with
5560
another name, you need to declare them as variable first.
@@ -61,7 +66,17 @@ arg1, arg2 = nil
6166
return arg1 + ' == ' + arg2
6267
}
6368
}.to_json
64-
#=> "{\"equality\":function(arg1, arg2) { arg1 + ' == ' + arg2 }}"
69+
#=> "{\"equality\":function(arg1, arg2) { return arg1 + ' == ' + arg2 }}"
70+
```
71+
72+
### Use Case
73+
74+
One example use case is [ApexCharts.RB](https://github.com/styd/apexcharts.rb)
75+
formatter. You can add tooltip formatter like this:
76+
77+
```erb
78+
<%= line_chart data, tooltip: {y: {formatter: function(val) { return '$' + parseFloat(val).toLocaleString(); }}} %>
79+
6580
```
6681

6782

0 commit comments

Comments
 (0)