1
1

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.

Rubyの基礎知識・文法について

Posted at

背景

Rubyの基礎知識を備忘録としてのこします。

基礎文法


出力の仕方
puts 文字列or値

コメントアウト
# コメントアウト

文字列の連結
文字列 + 文字列

変数の定義
変数名 = 

変数展開
文字列の中に#{変数名}とする。

if
if 条件式
    処理
elsif 条件式
    処理
else
    処理
end

比較演算子
a==b  # aとbが等しい
a!=b  # aとbは異なる

配列
配列名=[値1,値2,値3]

each
配列名.each do |変数名|
    処理
end

ハッシュ
ハッシュ名={キー1:値1,キー2:値2,}

メソッドの定義方法
def メソッド名
    処理
end

クラスの定義方法
class クラス名(大文字で始める)
    処理
end

インスタンスを生成
変数名=クラス名.new

クラス内で変数を作る方法
attr_accessor :変数名
インスタンスを生成すると同時に生成される

クラス内で変数を作る
self.変数名

他ファイルでクラスを使う方法
require ./ファイル名"

クラスの継承方法

class 子クラス<親クラス

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?