2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第14回つぶやき勉強会 Ruby と Javascript の スコープとか

Posted at
1 / 9

ruby's scope gate は、 class / module / def という話をしました。


つまり ブロックはクロージャであり、以下のようにできる


def hoge
  x = 'outer'
  for i in [3,4,5] do
    return x
  end
end
hoge
=> 'outer'

第1問
def hoge
  for i in 3..5 do
    x = 'x-men'
  end
  return x
end
hoge #=> これは?

第2問
def hoge
  3.times do
    x = 'x-men'
  end
  return x
end
hoge #=> これは?

第3問
var hoge = function(){
  function inner(){
    x = 'x-men'
  }
  inner()
  return x
}
hoge() //=> これは?

解答

  1. x-men
  2. error
  3. x-men

3 はひっかけです。var x で宣言していれば、外からは呼べません。


おまけ

true が返る行をすべておしえてください。

hoge = false && true
hoge = false and true
hoge = false || true
hoge = false or true
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?