@@ -32,14 +32,19 @@ And then execute:
32
32
``` ruby
33
33
{
34
34
speed: function(val) {
35
- return parseFloat(val).toLocaleString();
35
+ return ' $ ' + parseFloat(val).toLocaleString();
36
36
}
37
37
}.to_json
38
- # => "{\"speed\":function(val) { return parseFloat(val).toLocaleString(); }}"
38
+ # => "{\"speed\":function(val) { return '$' + parseFloat(val).toLocaleString(); }}"
39
39
```
40
40
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 ` ; ` .
43
48
44
49
If you try the above example on your REPL (e.g. ` irb ` or ` pry ` ) it won't
45
50
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.
49
54
50
55
Well, technically, it's not a JavaScript function. It's a Ruby method named
51
56
` 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,
53
58
` val ` needs to already exist before you call it, and it is. Both ` function ` and
54
59
` val ` are already a method of an object. If you want to use arguments with
55
60
another name, you need to declare them as variable first.
@@ -61,7 +66,17 @@ arg1, arg2 = nil
61
66
return arg1 + ' == ' + arg2
62
67
}
63
68
}.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
+
65
80
```
66
81
67
82
0 commit comments