問題 Ruby 編
zsh
$ ruby hoge.rb
# => これは何ですか?
# (以下に hoge.rb を記載)
hoge1.rb
a = 1 + 2
p a
hoge2.rb
a = 1
+ 2
p a
hoge3.rb
a =
1
+ 2
p a
hoge4.rb
a
=
1
+ 2
p a
解答 Ruby 編
zsh
$ for i in 1 2 3 4; do
for> echo "-- run hoge${i}.rb --"
for> ruby hoge${i}.rb
for> done
-- run hoge1.rb --
3
-- run hoge2.rb --
1
-- run hoge3.rb --
1
-- run hoge4.rb --
hoge4.rb:2: syntax error, unexpected '=', expecting end-of-input
問題 JavaScript 編
zsh
$ node piyo.js
# => これは何ですか?
piyo1.js
a = 1 + 2
console.log(a)
piyo2.js
a = 1
+ 2
console.log(a)
piyo3.js
a =
1
+ 2
console.log(a)
piyo4.js
a
=
1
+ 2
console.log(a)
解答 JavaScript 編
$ for i in 1 2 3 4; do
for> echo "-- run piyo${i}.js --"
for> node piyo${i}.js
for> done
-- run piyo1.js --
3
-- run piyo2.js --
3
-- run piyo3.js --
3
-- run piyo4.js --
3
解説
ASI ...Automatic Semicolon Insertion
ref. http://www.ecma-international.org/ecma-262/5.1/#sec-7.9
ref. http://asi.qfox.nl/
おまけ問題
omake.js
console.log(1)
[3,4,5].forEach((val, index) => {
console.log(val)
})
zsh
node omake.js
=> これは何?
おまけの解答
zsh
$ node omake.js
1
/Users/mochizuki/Desktop/tweet/omake.js:3
[3,4,5].forEach((val, index) => {
^
TypeError: Cannot read property '5' of undefined
at Object.<anonymous> (/Users/mochizuki/Desktop/tweet/omake.js:3:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
行末のセミコロンをつけないと、このようなエラーに遭遇できます。回避方法としては、
回避策
;[3,4,5].forEach((val, index) => {
console.log(val)
})
などのように、行頭にセミコロンなどをつけるとOKです。