Table of Contents

Programming Languages

Ruby

References


Framework

Framework Memo
Ruby on Rails http://www.rubyonrails.org/
Sinatra http://www.sinatrarb.com/
Padrino


Installing Ruby

https://www.ruby-lang.org/en/installation/

Linux




Grammar

Basic

Print

print "foo"   # not line break
puts "foo"    # line break
p 123         # for debug

Variables

a = 123
a = "abc"

foo   # local variable
@foo  # instance variable
@@foo # class variable
$foo  # global variable
FOO   #

num = 1
num = 1.234
num = 100_000_000

num = 1 + 1
num = 1 - 1
num = 1 * 2
num = 5 / 2    # 2
num = 5.0 / 2  #2.5
num = 5 % 2    #1

i += 1
i -= 1

str1 = 'abc'
str2 = "def"
str3 = "a\tbc\n"
str4 = "#{str1} def"  #=> "abc def"

str1 = "aaa" + "bbb"
str2 = ["aaa", "bbb", "ccc"].join(",")

record = "aaa,bbb,ccc".split(/,/)

substr = "abcd"[0, 2]   # "ab"

idx = "abcd".index(/bc/)

Array

ary = [100, 200, 300]

a = ary[0]   #=> 100
b = ary[1]   #=> 200

ary[0] = 1
ary[1] = 2

ary = [1, 2, 3]
a = ary.shift   #=> a=1、ary[2,3]
ary.unshift(5)  #=> ary[5,2,3]
b = ary.pop     #=> b=3, ary[5,2]
ary.push(9)     #=> ary[5,2,9]

Conditionals

if

if a == 1
  abbr
elsif b == 2
  abbr
else 
  abbr
end

while

i = 0
while i < 5
  abbr
  i += 1
end

subroutine

def sum(x, y)
  return x + y
end

p sum(1,2)        #=> 3


File

Read

str = File.read("foo.txt")

Write

File.open("out.txt", "wb") do |f|
  f.write str
end

 # ruby foo.rb a.txt b.txt
 p ARGV     #=> [a.txt, b.txt]