Last active
December 14, 2018 15:01
-
-
Save hanachin/77a7d9f29ee3adc806c6c45d70767053 to your computer and use it in GitHub Desktop.
RubyでJavaScriptのTemplate literalsの真似をする ref: https://qiita.com/hanachin_/items/f1f822ff2ecd5f384eca
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % gem i javascript_template_literals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "javascript_template_literals" | |
| using JavaScriptTemplateLiterals | |
| a = 5 | |
| b = 10 | |
| puts `Fifteen is ${a + b} and | |
| not ${2 * a + b}.` | |
| # Fifteen is 15 and | |
| # not 20. | |
| def my_tag((strings, person_exp, age_exp)) | |
| str0 = strings[0] | |
| str1 = strings[1] | |
| if age_exp > 99 | |
| age_str = "centenarian" | |
| else | |
| age_str = "youngster" | |
| end | |
| `${str0}${person_exp}${str1}${age_str}` | |
| end | |
| person = "Mike" | |
| age = 28 | |
| puts my_tag`That ${ person } is a ${ age }` | |
| # That Mike is a youngster |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module JavaScriptTemplateLiterals | |
| refine(Object) do | |
| def `(str) | |
| # 普通は文字列として振る舞う | |
| ret = "foo2000bar" | |
| # to_aryメソッドがあるため | |
| # tagメソッドに渡されたときはタグっぽく振る舞う | |
| ret.define_singleton_method(:to_ary) do | |
| [["foo", "bar"], 2000] | |
| end | |
| ret | |
| end | |
| end | |
| end | |
| using JavaScriptTemplateLiterals | |
| def my_tag((strings, exp)) | |
| "#{strings.join}#{exp}" | |
| end | |
| puts `foo${1000 * 2}` | |
| # foo2000bar | |
| puts my_tag `foo${1000 * 2}` | |
| # foobar2000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module JavaScriptTemplateLiterals | |
| refine(Object) do | |
| def `(str) | |
| # ここでJavaScriptのTemplate literals風の動作をさせる | |
| "bar" | |
| end | |
| end | |
| end | |
| using JavaScriptTemplateLiterals | |
| puts `foo` | |
| # bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "binding_of_caller" | |
| module JavaScriptTemplateLiterals | |
| refine(Object) do | |
| def `(str) | |
| binding.of_caller(1).eval('x') | |
| end | |
| end | |
| end | |
| using JavaScriptTemplateLiterals | |
| x = "foo" | |
| puts `bar` | |
| # foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "binding_of_caller" | |
| module JavaScriptTemplateLiterals | |
| refine(Object) do | |
| def `(str) | |
| b = binding.of_caller(1) | |
| str.gsub(/\$\{([^\}]*)\}/) { b.eval($1) } | |
| end | |
| end | |
| end | |
| using JavaScriptTemplateLiterals | |
| foo = "bar" | |
| puts `${foo}` | |
| # bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module JavaScriptTemplateLiterals | |
| refine(Object) do | |
| def `(str) | |
| # もしここで返した値がtagっぽいメソッドに渡るなら配列を返す | |
| [["foo"], 1, 2] | |
| end | |
| end | |
| end | |
| using JavaScriptTemplateLiterals | |
| def bar(strings, exp1, exp2) | |
| "bar" | |
| end | |
| puts bar(*`${foo}`) | |
| # bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module JavaScriptTemplateLiterals | |
| refine(Object) do | |
| def `(str) | |
| # ここで返した値がtagっぽいメソッドに渡るかどうか、判断がつかない... | |
| [["foo"], 1, 2] | |
| end | |
| end | |
| end | |
| using JavaScriptTemplateLiterals | |
| def bar(strings, exp1, exp2) | |
| "bar" | |
| end | |
| # これ↓がいけてない | |
| puts bar(*`${foo}`) | |
| # bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def foo((a, b, c)) | |
| a + b + c | |
| end | |
| puts foo([1, 2, 3]) | |
| # 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def foo((a, b, c)) | |
| a + b + c | |
| end | |
| obj = Object.new | |
| def obj.to_ary | |
| [1, 2, 3] | |
| end | |
| puts foo(obj) | |
| # 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def my_tag((strings), exp1, exp2) | |
| # ... | |
| end | |
| a = 44 | |
| b = 23 | |
| my_tag`a + b = ${a + b}, a * b = ${a * b}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment