Rubyのテスト用にC++に続いて作ってみました。
配列と数値変換、関数、ループ、if文を軽くおさらいしています。
サンプルのほとんどはRuby 3.02のリファレンスから、動作確認はしていません。
あとcontinueはnext 絶対値はabs
テキスト版
puts str.to_i
puts int.to_s
p "a".ord
p 65.chr # => "A"
p "stRIng? STring.".upcase
p "STRing?".downcase
p "11111111".to_i(2)
p "FF".to_i(16) #=> 255
p 255.to_s(2) #=> "11111111"
p 255.to_s(16) #=> "ff"
p " a \t b \n c".split(' ') # => ["a", "b", "c"]
enum = [1, 2, 4, 2].each
enum.count # => 4
enum.count(2) # => 2
a = ["a", "b", "c"]
b = Array.new(a) # 複製
ary = Array.new(3){ "foo" }
p ary #=> ["foo", "foo", "foo"]
[1, 1, 2, 3] & [3, 1, 4] #=> [1, 3] #同じ数は1つに
p [1, 2, 3] * 3 #=> [1, 2, 3, 1, 2, 3, 1, 2, 3]
p [1,2,3] * "," # => "1,2,3"
ary = [1]
ary << 2 # 追加
p ary # [1, 2]
a = [ "a", "b", "c", "d", "e" ]
a[0..1] #=> ["a", "b"]
strs = ["a", "b", "c"]
for str in strs
puts str
end
while i < 3
...
end
def sum(a, b)
a + b
end
answer = sum(2, 3)
if age >= 12 then print "adult fee\n" else print "child fee\n" end