Ruby 2.0.0-preview2 released
blade.nagaokaut.ac.jp
blade.nagaokaut.ac.jp
As far as I understand it the current spec is to have "using" be available only to main and only apply in the file scope. IMHO this makes the feature nearly useless.
The thing I like the most about ruby is how much thought is put into code readability and programming experience.
My favorite features of this release:
1. Keyword arguments: I can stress how important this is for code readability and also prevents some bugs.
2. Refinements: This is going to make code easier to maintain and more "custom". I can't wait to add a few things we constantly use on String etc.
I think that the latest refinement spec addresses at least some of the issues he raised.
I thought Matz was going to drop refinements for 2.0 for now?
Hashes are hideously expensive, though: For every method call, an entire object has to be populated and hashed. The hashing is fast enough for symbol keys, sure, but it's still a whole data structure that must be allocated and later garbage collected.
Since Ruby hashes are mutable and there is no way to track the mutation across methods, the compiler cannot optimize single-use hash literals into a constant. (When all keys and literals are values it could pre-allocate a structure that is copy-on-write internally, but to my knowledge it doesn't do this.) For example, this is a typical pattern:
def generate_useless_stuff(options = {})
path = options[:path] || @default_path
value = options[:value]
File.open(path, "w") { |f| f << ("*" * value) }
end
generate_useless_stuff(value: 42)
In a statically-typed language, the compiler would know that {value: 42} was created once and never reused, and since the value is a literal, the :value key could simply have been plugged directly into the value variable. Also, the lookup of :path is completely unnecessary in this case.Often all the options are optional, and even then a call to generate_useless_stuff() will generate an empty hash. This is why I tend to write methods, when performance-sensitive, as:
def generate_useless_stuff(options = nil)
path = options[:path] || @default_path if options
value = options[:value] if options
File.open(path, "w") { |f| f << ("*" * value) }
end
Of course, this decreases allocation while adding complexity.So yes, I absolutely agree that it's amazing. :-) I think the core team should have considered it earlier.
Anyway, everything adds up. Even for webapps in Rails or Sinatra, the sheer number of method calls in a single template may contribute significant overhead to the rendering of a page. Which means that shaving a few microseconds off a method call may in fact boost performance measurably. I am hoping this will be the case with named arguments.
On a slightly off-topic note, what's the best way to dive into Ruby coming from (strong) Python background? I bet the syntax wouldn't take more than a weekend to get used to but I'm more interested in the less trivial stuff (semantics, idiomatic style, dev environment, 3rd party lib ecosystem etc.)
It's nice to see that we won't have to rely on smart hacks though and that real keyword arguments are now available, but it's nowhere near earth-shattering.
As for refinements, it's theoretically a very nice feature, but it seems that the details are still hazy and the green-ness of the feature raises more eyebrows than it solves problems.
For anyone else that wasn't aware what Dtrace does.
Wikipedia Link: http://en.wikipedia.org/wiki/DTrace
Put in the right hands, this will prove to be a deadly weapon.
<meta charset="utf-8">
Either of those two options should fix the page, but I assume Japanese browsers have different default charset than ours since no one has fixed it yet.num2int.c:82:21: error: expected ')'
sprintf(buf, "%"PRI_LL_PREFIX"d", NUM2LL(num));
^Source: https://bugs.ruby-lang.org/issues/6265
Btw, PRI_LL_PREFIX is defined in Ruby's config.h.
That being said, I'm not a language or VM guy, so take what I say with a grain of salt.