9cubed
ブログ | Tailwind | Vite | Python | Node.js | Linux | PowerShell | その他 | 将棋ウォーズ | 歌の練習
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

新着一覧

  • SCSS のインストールVite
  • Tailwind CSS のプロジェクトの作成Tailwind
  • TypeScriptのプロジェクトの作成Vite
  • Flask のインストールと動作確認Python
  • 簡易Webサーバーの作成Python
  • pipeline で文章の生成Python
  • pipeline で文章の要約Python
  • 音声から文字起こしPython
  • Node.js のインストールNode.js
  • .ps1(PowerShellスクリプト)を実行可能にするPowerShell

アーカイブ

  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08

以前のカテゴリー一覧

  • 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.

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