4 ms·
_why's version of this site was my first exposure to Ruby. I was a convert (from Java) as soon as I got to map. %w{apple pear banana}.map {|f| puts "#{f} is a
by patio11 17y ago
_why's version of this site was my first exposure to Ruby. I was a convert (from Java) as soon as I got to map.
%w{apple pear banana}.map {|f| puts "#{f} is a fruit."}
This experience shows one of the fundamental truths of software marketing, incidentally: anything you can do to reduce the barriers to trying the software out increases uptake dramatically. Eliminating the install is powerful, powerful stuff.
- harpastum 17y agoWouldn't it make more sense to use each rather than map for that example? %w{apple pear banana}.each {|f| puts "#{f} is a fruit."} => apple is a fruit => pear is a fruit => banana is a fruit Map is for creating a new array with each of the results in it, like this: %w{apple pear banana}.map {|f| "#{f} is a fruit."} =>["apple is a fruit.", "pear is a fruit.", "banana is a fruit."] which would return an array with each of the strings in it.
- jamesbritt 17y agoQuite right. puts %w{apple pear banana}.map {|f| "#{f} is a fruit."}.join(' And ')
- axod 17y agoIt was useful for me in an opposite way :) It showed me why I hate Ruby syntax. The above just looks horribly ugly. A mishmash of special characters. Not to mention special characters in strings (eugh). The probability of an undetectable typo in the code you paste is pretty high. Sure, it's not quite up there with regexp syntax, but it's getting close.
- oscardelben 17y agoIt's interesting because ruby is designed to be easy to read, and in my experience it's the programming language where I have less syntax errors compared to other languages like c, clojure, lisp, etc (which can be prevented 99% of times with an editor)
- axod 17y agoIt doesn't look any easier to read that any other language to me. Looks harder to read than many. So if it is indeed 'designed to be easy to read', I think the failed.
- Pistos2 17y agoFWIW, I've been coding Ruby going on 7 years now (well over 100,000 lines of Ruby across all projects, I'd wager), and my memory estimates fewer than 5 instances of "undetectable typo". Generally, the typos I made and tried to run produced very obvious, visible and traceable symptoms (i.e. a precise and accurate stack trace). I was asked in an interview once (by a skeptic that didn't know Ruby) if it weren't a problem that mistyping a variable name could produce a bug that would be difficult to detect and debug. I informed him that, in practice, it is essentially a non-issue. There are two aspects of Ruby (among others, I'm sure) that help protect against the sort of hidden bugs you're worried about: 1) You cannot reference a variable that has not been defined yet. Example: % ruby supercalifragilisticexpialidocious = "something quite attrocious" puts "I'm thinking of #{supercalifragilisticexpialodocious}." ^D -:2: undefined local variable or method `supercalifragilisticexpialodocious' for main:Object (NameError) 2) Ruby will not silently or automatically convert or cast data types. (Where it seems to do so, in actual fact underlying library code is explicitly calling the conversion methods (#to_s, #to_i, #to_f, etc.)) Example: % ruby count_str = "8" count = count_str.to_i puts count < 9 puts count_str < 9 ^D true -:4:in `<': comparison of String with 9 failed (ArgumentError) Contrast that with, say, Perl: % perl use strict; my $count_str = "8"; my $count = 0 + $count_str; print $count < 9; print "\n"; print $count_str < 9; print "\n"; ^D 1 1 Combine all of the above with good testing practices, and your bugs turn out to be quite detectable on the whole.
- eru 17y agoRuby seems similar to Python in this regard. (Though Python seems a bit more concerned about special stuff in strings.)
- axod 17y agoIt's not just about potential bugs though. It's readability, maintainability etc. For me, the original code pasted is horrible in terms of readability. Sure, it's concise, but it's lots of small characters that you could misread etc.
- jamesbritt 17y ago