Try Ruby (in your browser, now with 1.9)
tryruby.org
tryruby.org
%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.
%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. puts %w{apple pear banana}.map {|f| "#{f} is a fruit."}.join(' And ')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.
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.To each his own, but have you spent much time coding Ruby and encountering this problem?
I ask because it's not a problem I've had or hear from people who have been using the language. It seems to be be an issue expected by people who have not yet spent much time using Ruby (much like concerns over the lack of static typing).
From a distance it may seems as though things would get confusing, but in actual practice this seems not to be the case.
Could be a selection bias, but I would like to hear from people who have truly used Ruby in earnest and stopped because of repeated annoyances.
Also bear in mind that, in Ruby, almost all blocks can be delimited with a "do/end" pair rather than set braces.
That helps somewhat, but you can certainly assign to a variable you haven't defined. Detect that.
> Contrast that with, say, Perl....
If you'd used the warnings pragma, Perl would happily have warned you about any problems numifying the string. In this case, there are no problems numifying the string, so there's no warning.
(As Perl is an operator-oriented language, the use of syntax which provides numeric or boolean or string context on values indicates programmer intent.)
JS emulated would be good if it was just a tutorial, but it's a bit more than that.
The performance will be horrible, of course, but once that happens some other person will come along and run a subset of Rails (or some other Ruby web framework) with an evented db backend on it (for async callbacks to the server over HTTP) to run an app in the browser.
Which would be pretty cool, though not useful. (But don't let that stop you, whoever you are!)
So, assuming that I'm correct, what you would be left with is the one-time cost of parsing. And how costly is parsing generally? (I am keeping in mind that Ruby may not be general)
Some sort of Flash based Ruby VM that you can interact with through JavaScript would make it rather interesting though..
_why the lucky stiff did a lot for Ruby evangelism. This site combined with the Poignant Guide turned a lot of curious folks into converts.
I know this stuff despite being a Python fanatic. I have great respect for what _why did for the Ruby and programming communities in general.
I agree that this is the case, but only because Ruby exists. If you haven't tried it then really, please do, I think you'll like it.
Ruby doesn't even attempt to solve this problem. You can tell because if it didn't already have try/catch or begin/rescue or case/when, adding them yourself would be impossible.