[ create a new paste ] login | about

Link: http://codepad.org/WxAfyWOX    [ raw code | output | fork | 1 comment ]

joshua_cheek - Ruby, pasted on Oct 26:
#Learning Ruby chapter 3

#using conditional
x = 256
puts "1 x=256" if x == 256
if x == 256 then puts "2 x=256" end
if x == 256 : puts "3 x=256" end

x = true
y = false
if x and !y then puts "4 and == && =>true" end
if x && !y then puts "5 and == && =>true" end
if x or y  then puts "6 or == || =>true" end
if x || y  then puts "7 or == || =>true" end
if !y then puts "8 not == ! =>true" end
if not y then puts "9 not == ! =>true" end

#what is true / false?
somevariable = "whatever"
if somevariable then puts "10 everything is true" end
somevariable = nil
if somevariable then puts "11 except nil and false" end

#elsif
x = 5
if    x == 0: puts "12 zero"
elsif x == 5: puts "12 five"
else          puts "12 IDK"
end


#case / switch
puts case x
  when 0...5  : "13 less then 5"
  when 5...10 : "13 5<=x<10"
  when 10..20 : "13 10<=x<=20"
  else          "13 IDK"
end
  
  
#while / until
print "14 "
i=0
begin
  print i+=1
end while i < 3

while i < 6
  print i+=1
end

print "\n15 "

i=0
print i+=1 until i > 5

puts


#other loops
print "16 "
for i in 1..6
  print i
end

print "\n17 "
6.times{ |i| print i }

print "\n18 "
5.upto(9){ |i| print i }

print "\n19 "
9.downto(5){ |i| print i }
puts

#begin and end
BEGIN { puts "20 I AM FIRST!" }
END {puts "21 I AM LAST!" }


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
20 I AM FIRST!
1 x=256
2 x=256
3 x=256
4 and == && =>true
5 and == && =>true
6 or == || =>true
7 or == || =>true
8 not == ! =>true
9 not == ! =>true
10 everything is true
12 five
13 5<=x<10
14 123456
15 123456
16 123456
17 012345
18 56789
19 98765
21 I AM LAST!


Create a new paste based on this one


Comments:
posted by joshua_cheek on Nov 15
My notes from when I read Learning Ruby
(I didn't expect anyone else to look at them, but a friend asked for info on Ruby)
reply