9cubed
ブログ | Tailwind | Vite | Python | MariaDB | Node.js | Linux | PowerShell | Docker | Git | その他 | 将棋ウォーズ | 歌の練習
Ruby

Ruby のお勉強 - if -

公開日:2014-12-21
更新日:2019-05-13

if

if true
  print "1"
  print "2"
end
##src|12 ##/e## then はつけなくても良い。付けても良い。


if false
  print "true"
else
  print "false"
end
##src|false ##/e## else は普通に使える。


if true then print "a" end
##src|a ##/e## then を使えば、1行で書くことが出来る。then を抜くとコンパイルエラー。


if -1    then print "true\n" end 
if  0    then print "true\n" end
if  1    then print "true\n" end
if nil   then else print "false\n" end
if false then else print "false\n" end
##src|true true true false false ##/e## nil と false 以外は、true として扱われる。


s = "test"
if s then print "true\n" end
s = ""
if s then print "true\n" end
##src|true true ##/e## 文字列の場合、空文字""でも true として扱われる。


a = 5
a = a + 1 if a == 5
p a
6
英語と同じ書き方をできる。
もし a == 5 なら、a = a + 1 を実行。


array1 = ["a", "b", "c"]
array2 = ["a", "b", "c"]
if array1 == array2 then p "OK" end
array2.push("d")
if array1 == array2 then p "OK" end
"OK"
配列は中の値が全て同じの時に true となる。


class TestClass
end

obj1 = TestClass.new
obj2 = TestClass.new
if obj1 != obj2 then p "NG" end
"NG"
オブジェクトの場合は値ではなく参照の比較となる。


YouTube X

新着一覧

  • テーブル結合(CROSS JOIN、INNER JOIN、LEFT JOIN)MariaDB
  • 楽観ロック・悲観ロックMariaDB
  • カレントリードMariaDB
  • インデックスMariaDB
  • 論理削除(ソフトデリート)MariaDB
  • awk(オーク)の使い方についてLinux
  • NOT NULL 制約と NULL を許容した時の動作MariaDB
  • 外部キー制約MariaDB
  • MySQL と MariaDB の関係MariaDB
  • Docker で PostgreSQL のコンテナの使用Linux

アーカイブ

  • 2026/01
  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08
  • /00

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲