With Ruby 1.9.3 nearing here's a casual look at some of the upcoming changes in 1.9.3rc1.
Ruby's license is no longer a dual license with GPLv2. Now it's a dual license with a 2-clause BSDL.
According to Yui Naruse on the ruby-core mailing list this means that:
- Ruby's license is now compatible with GPLv3
- Ruby's code can be ported to BSDL code
For more information on Ruby's license refer to COPYING.
New EncodingsRuby 1.9.3 has added support for the following encodings:
File/IO: putc now supports multibyte characters
In Ruby 1.9.2 and lower putc couldn't handle multibyte characters. In fact, it would only handle the first byte:
With 1.9.3 you don't need to worry about it:
File/IO: Shorthand for writing files with IO.write/binwrite
Ruby's IO has a class-level read method. You give it a filename and it will handle opening, reading, and closing the file for you. It felt strange that there was no equivalent for writing files. With 1.9.3 there is.
Here's what you used to have to write:
And here it is with 1.9.3:
These methods also take an optional third argument which represents the offset at which to start writing.
File/IO: Forget about /dev/null
Prior to 1.9.3 if you wanted to interact with a NULL device like /dev/null you had to manage that yourself in code:
With 1.9.3 knowing the specific location of the NULL devise has been moved in the File::NULL constant so you no longer need to care where the device is. Here's a 1.9.3 rewrite of the above snippet:
This makes for more portable code.
Generating random numbers with a Range
Kernel.rand now supports taking a numeric Range.
One thing to note is that this is inclusive, unlike how Kernel.rand behaves when just passing in a numeric limit.
Also, Random.rand now takes a numeric range as well.
Module/Class: Constants get public/private visibility
Constants have always had pubic visibility by default, but in 1.9.3 you're given the ability to be explicit about their scope. They can be made public or private. This can be done with Module#public_constant and Module#private_constant:
You can use public_constant and private_constant with Class-level constants as well since Class inherits from Module, ie:
The basic rule for accessing constants is this: public constants are available with and without a receiver, private constants are only available without a receiver.
Here's an example:
Straight from the Ruby 1.9.3 documentation:
Byte Reference -- If passed a single
Fixnum, returns a substring of one byte at that position. If passed two
Fixnumobjects, returns a substring starting at the offset given by the first, and a length given by the second. If given a
Range, a substring containing bytes at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns
String#prepend has been added which allows you to prepend content to the beginning of a String:
Time#strftime supports %:z and %::z
Time#strftime now supports representing the hour and minute offset from UTC with %:z:
Using %::z you can represent the hour, minute, and seconds offset from UTC with %::z: