4 ms·
For me it was strange that Ruby used "end" as the block ending delimiter in a world and era where so many other languages were using "}" quite effectively, than
by mkramlich 16y ago
For me it was strange that Ruby used "end" as the block ending delimiter in a world and era where so many other languages were using "}" quite effectively, thank you very much. And Python was showing you didn't even need that much. It felt like a throwback to an earlier era of programming, like COBOL or Pascal.
- steveklabnik 16y agoYou can use {} instead of do/end if you'd like.
- mkramlich 16y agogreat! wonder why most people don't use it? I keep seeing "end" in almost every chunk of Ruby code I see on the web.
- steveklabnik 16y agoThe general convention is that {} is for one line blocks, and do/end for multiline. [1,2,3].each{|n| puts n } versus [1,2,3].each do |n| m = n + 1 puts m end The reason? do/end is much prettier than {}, but {} is really nice for chaining... [1,3,2].collect{|n| n + 2 }.sort
- ShardPhoenix 16y agoI find it easier to type "end" than "}".
- mkramlich 16y agobut it's 3 characters versus 1. is it easier because you don't have to hit SHIFT?
- yxhuvud 16y agoThere are a lot stranger stuff hiding in Ruby. Flip-flops. Do I need to elaborate?
- avar 16y agoRuby got its flip-flop operator from Perl, which in turn got it from sed and awk. It can be really useful for keeping simple state, e.g.: # parse mail messages while (<$email>) { $in_header = 1 .. /^$/; $in_body = /^$/ .. eof; if ($in_header) { # do something } else { # in body # do something else } } And other examples at http://perldoc.perl.org/perlop.html#Range-Operators http://perldoc.perl.org/perlop.html#Range-Operators