◼︎ 概要
Rubyを学ぶために基本的な使い方をまとめます
前提として他の言語とかで変数、ループ、条件分岐とか基本的なものは知っているけどRubyとしてはどう書くのか学びたい人向けの入門(仮)になります
◼︎ 基本構文
出力
print "hello world!" # 出力:hello world! (改行無し) puts "hello world(puts)" # 出力:hello world! (改行有り) p "hello world(p)" # 出力:"hello world(p)" (データの形式がわかる)
複数行コメント
=begin 複数行コメント 複数行コメント =end
変数代入
msg = "hello world"
定数
# 大文字から始まると定数 # 上書きしようとするとエラーになる ADMIN_EMAIL = "hoge@gmail.com"
数値演算
# 参考:http://docs.ruby-lang.org/ja/2.3.0/class/Numeric.html x = 1 + 2 - 3 * 4 / 5 % 6 x += 5 # x = x + 5 puts x # 出力:6
文字列操作
# 参考:http://ref.xaio.jp/ruby/classes/string # 参考:http://docs.ruby-lang.org/ja/2.3.0/class/String.html p "hello world".length # 出力:11
文字列と変数
str = 'world!'
puts "hello #{str}" # 出力:hello world!
puts "hello\tworld!\n\n" # 出力:hello world! (2回改行がある)
配列
arr = [] # 初期化 arr = [1, 3, 9] p arr[0] # 出力:1 p arr[-1] # 出力:9 p arr[0..2] # 出力:[1, 3, 9] # 0~2番目の要素を取得 p arr[0...2] # 出力:[1, 3] # 0~2番目未満の要素を取得 p arr[0..2][1] # 出力:3 p arr[1, 1] # 出力:[3] # 1番目の要素から1つ取得する p arr.size # 出力:3 p arr.sort # 出力:[1, 3, 9] p arr.sort.reverse # 出力:[9, 3, 1] p arr.push(8) # 出力:[1, 3, 9, 8]
ハッシュ
has = {} # 初期化
has = {
'name' => 'tomsato',
'id' => 100
}
p has # 出力:{"name"=>"tomsato", "id"=>100}
p has.size # 出力:2
p has.keys # 出力:["name", "id"]
p has.values # 出力:["tomsato", 100]
オブジェクトの変換
num = 8 str = '5' p num + str.to_i # 出力:13 # strを数値に変換して演算 p num.to_s + str # 出力:"85" # numを文字列に変換して文字列連結
条件分岐
# >, <, >=, <=, ==, !=
# &&, ||,!
num = 3
if num > 1
p 'if true' # 出力:"if true"
else
p 'if false'
end
三項演算子
num = 10 p num > 3 ? 'ture':'false' # 出力:"true" p num < 3 ? 'ture':'false' # 出力:"false"
true, false
=begin falseになるもの:false, nil(オブジェクトが存在しない) trueになるもの:それ以外(0, ''を含む) =end p 'true 0' if 0 # 出力:"true 0" p 'true emp' if '' # 出力:"true emp"
ループ処理
# times
3.times do
puts "hello" # 出力:"hello" (3回出力)
end
3.times do |i|
puts i # 出力:0から2を順に出力
end
# while
i = 0
while i < 3 do
puts i # 出力:0から2を順に出力
i += 1
# break:ループを抜ける
# next:ループを1回スキップ
end
# for
for i in 0..2 do
puts i # 出力:0から2を順に出力
end
# each1
[5, 8, 4].each do |e|
puts e # 出力:5,8,4を順に出力
end
# each2
{"hoge1" => 5, "hoge2" => 8, "hoge3" => 4}.each do |key, value|
puts "#{key}: #{value}" # 出力:hoge1: 5など順に出力
end
関数
def testFunction(name = 'test')
return name
end
puts testFunction() # 出力:"test"
puts testFunction('aiu') # 出力:"aiu"
クラス
class Testttt
# @で始まる変数はインスタンス変数(このクラスの例だと@name)
# @で始まってclass定義式内で定義したものはクラスインスタンス変数(このクラスの例だと@str)
# @@から始まる変数はクラス変数(このクラスの例だと@@count)
# 全てのインスタンスで使い回すことができる
@@count = 0
# 基本的にクラス変数と同じだが継承されたクラスからは参照できない
@str = 'test string'
# オブジェクト(インスタンス)を作る際に必ず実行される初期化処理
def initialize(name = 'hoge')
# @から始まる変数はインスタンス変数
# インスタンス毎に値を保持できる
@name = name
@@count += 1
end
def printName
puts "name: #{@name}"
end
def Testttt.printStr
puts "str: #{@str}"
end
# クラスメソッド
def Testttt.printCount
# インスタンスを作らなくても直接呼び出せる
puts "count: #{@@count}"
end
end
hoge = Testttt.new()
tom = Testttt.new('tom')
hoge.printName() # 出力:name: hoge
tom.printName() # 出力:name: tom
Testttt.printCount() # 出力:2 # インスタンスが2回作られたのでcountには2が入る
Testttt.printStr() # 出力:str: test string
継承
class Hogeeee < Testttt
# Testtttクラスを継承したHogeeeeクラス
end
Hogeeee.printCount() # 出力:2
Hogeeee.printStr() # 出力:str: # strはTesttttクラスのクラスインスタンス変数のため継承先からは参照できない
アクセサ
# 要はsetterとgetter
class Person
# getter, setterを簡単に書ける
attr_accessor :name
# getterだけ
attr_reader :id
# setterだけ
attr_writer :str
end
tom = Person.new()
tom.name = 'tom' # 出力:
puts tom.name # 出力:tom
コメントを書く
コメント一覧