// 10 different ways to display odd numbers 1 through 100 in Ruby
1
2 100.times do |i|
3 next if i % 2 == 0
4 puts i
5 end
6
7 50.times { |i| p i*2+1 }
8 (1..100).step(2) { |i| puts i}
9 1.upto(100) { |i| puts i unless i[0].zero? }
10 puts Array.new(50) { |i| i * 2 + 1 }
11
12 lambda { me = lambda { |x| p x; me.call(x+2) if x < 99 } ; me.call(1) }.call
13
14
15 rec = lambda { |v,l| p v; l.call(v+2,l) if v < 99 }
16 rec.call(-1,rec)
17
18
19 def odds(x=1)
20 p x
21 odds(x+2) if x < 99
22 end
23 odds
24
25 class Integer
26 def odd?
27 self[0].nonzero?
28 end
29 end
30 100.times { |i| puts i if i.odd? }
31
32
33 require 'delegate'
34 class OddNum < DelegateClass(Fixnum)
35 def initialize(value)
36 value |= 1
37 super(value)
38 end
39
40 def succ
41
42
43 OddNum.new(super)
44
45
46
47
48
49 end
50 end
51 (OddNum.new(1)..100).each { |i| puts i }