>> x = 4 => 4 >> puts 'This appears to be false.'unless x == 4 => nil >> puts 'This appears to be true.' if x == 4 This appears to be true. => nil >> if x == 4 >* puts 'This appears to be true.' >* end This appears to be true. => nil >>unless x == 4 >* puts 'This appears to be false.' >* else >* puts 'This appears to be true.' >* end This appears to be true. => nil >> puts 'This appears to be true.' if not true => nil >> puts 'This appears to be true.' if !true => nil
当你使用if或unless时,既可选用块形式(if condition, statements , end),也可选用单行形式(statements if condition)。
>> x = 1 => 1 >> x = x + 1 while x < 10 => nil >> x = x - 1 until x == 1 => nil >> x => 1 >> while x < 10 >* x = x + 1 >* puts x >* end 2 3 4 5 6 7 8 9 10 => nil