Haven't tried it, but the "LHS (some_dynamic_method) RHS" syntax does
seem a bit odd to me. You might try "LHS.send(some_dynamic_method,
RHS)". So something like:
puts (1..10).collect do |n|
n.send( (times =~ /^t/ ? '*' : '+'), number )
end.join(', ')
Personally, I don't like it though. You're making a RegExp match on
every iteration, when you only need the test once, and the Ternary
operation in the middle of all that isn't the easiest to read.
I'd recommend taking advantage of Ruby's functional nature instead:
print "(t)imes or (p)lus: "
times = gets
print "number: "
number = gets.to_i
calc = if times =~ /^t/
lambda { |n| n * number }
else
lambda { |n| n + number }
end
puts (1..10).collect(&calc).join(", ")
Other than that, I wouldn't worry about making it "simpler".