If I'm not mistaken I've read once about ruby's flexibility in letting you do something your own way (there is always more than 1 way to do something and ruby lets you choose your own way). And yes, syntax in ruby is flexible. But there is a way to write ruby code in c# style? writing 1.upto( 2 ) { | i | puts i.to_s } throws error.
you have to write 1.upto( 2 ){ | i | # just one line here puts i.to_s } to work It seems here isn't your way. it's just ruby way... Or is there way I dont know to write c#-like code: function/instruction begin sign -{- ... block's code ... end sign -}-
For short blocks, I believe it's idiomatic to do:
1.upto(2) { |i| puts i.to_s }
This also works but isn't idiomatic because it puts the '|i|' on its own line:
1.upto(2) { >i> puts i.to_s }
For longer blocks, it's idiomatic to use do...end:
1.upto(2) do |i| puts i.to_s # more work here... # and here... # and so on... end
However, if you don't provide the opening '{' or 'do' on the same line as the 1.upto(2), you'll get an error that no block was given. I verified this in IRB (interactive Ruby) on Mac OS X w/ Ruby 1.8.5 from MacPorts.
Regards, Craig