I'm a Ruby novice working my way through the pickaxe book, and I've got
what should be an easy syntax question. On page 84 of chapter 6, the
authors present the following program...
print "(t)imes or (p)lus: "
times = gets
print "number: "
number = Integer(gets)
if times =~ /^t/
calc = lambda {|n| n*number }
else
calc = lambda {|n| n+number }
end
puts ((1..10).collect(&calc).join(", "))
....Since everything is an object (right?), I thought I'd try a couple
of different ways to tighten that up. The guts of the first thing I
tried...
puts((1..10).collect{|n| n (times =~ /^t/ ? (*) : (+)) number }.join(",
"))
....results in syntax errors. Tossing in extra dots and ampersands in
several places didn't cure things for me. So, do I have just a simple
syntax error there, or am I making a fundamental Ruby mistake? I also
tried the following...
puts((1..10)
.(times =~ /^t/ ? collect {|n| n*number } : collect {|n| n+number })
.join(", "))
....which doesn't work either. Advice?
Thanks,
Greg Buchholz