SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
3.
Rubyに後置case文を追加
Rubyのcase文はCのswitchに相当
C Ruby
switch(var){
switch case var
case 1: when 1 then
puts(”case 1”); puts ”case 1”
break; when 2 then
case 2: puts ”case 2”
puts(”case 2”); else
break; puts ”default”
default:
default end
puts(”default”);
}
4.
Rubyに後置case文を追加
修飾子による制御
a=0,x=100,y=false
puts ”#{a=a+1}” while a<10
puts ”x=#{x}” if x==100
puts ”y=#{y}” unless y
z=(1/0) rescue puts ”zero div!”
ここにcase文も追加したい!
N=10, m=false
puts ”Hello” case n when (1..20)
puts ”Welcome” case when m
8.
Rubyに後置case文を追加
新しい文法規則を追加した結果
#追加した構文
puts”ok” case gets() when 'x','y','z'
#既存の構文
g = gets
puts”ok” if g=='x'||g=='y'||g=='z'
case gets
when 'x','y','z'
puts ”ok”
end
9.
#既存の構文
t = true
case t when true then puts”ok” end
#=>”ok”
#干渉しない!
10.
Rubyに後置case文を追加
#既存の構文
def say arg;p arg;end
say case option
when :ruby then s.to_ruby
when :marshal then ...
else s.to_yaml
end
#syntax error, unexpected 'n', expecting keyword_when
#なにそれ読めない! Rubyでおk