LoginSignup
1
0

More than 3 years have passed since last update.

【Ruby】if文の条件式で変数を代入する。

Posted at

個人メモです。

if(x = test)が何をしているかについて。rubyでは変数の条件式で代入が使える。

実例

if x = test
  p x
end

=> nil

if x = testで、変数testの値を変数xに代入している。

もし代入元の変数testがfalseなら結果はfalseになる。

p x
「p」は出力。変数xを出力するとの意味。



変数テストに値が存在すれば、if文の結果はtrueになる。

test = "テスト"

if x = test
  p x
end

=> "テスト"
1
0
1

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
1
0