fREWdiculous!
26 Dec
Exciting! It was apparently put up yesterday, on Christmas. What a cool gift right? I looked through the changed maintained my Mauricio and here are /my/ favorites.
New literal hash syntax [Ruby2]
.() and calling Procs without #call/#[] [EXPERIMENTAL]
You can now do:
Multiple splats allowed
1.9 allows multiple splat operators when calling a method:
Mandatory arguments after optional arguments allowed
Object#tap
Passes the object to the block and returns it (meant to be used for call chaining).
Module#attr is an alias of attr_reader
Use
to create a read/write accessor. (RCR#331)
Enumerable#cycle
Calls the given block for each element of the enumerable in a never-ending cycle:
Enumerable#group_by
Groups the values in the enumerable according to the value returned by the block:
Enumerable#drop
Without a block, returns an array with all but the first n elements from the enumeration. Otherwise drops elements while the block returns true (and returns all the elements after it returns a false value):
Enumerable#inject (#reduce) without a block
If no block is given, the first argument to #inject is the name of a two-argument method that will be called; the optional second argument is the initial value:
Enumerable#count
It could be defined in Ruby as
Therefore
Array#nitems
It is equivalent to selecting the elements that satisfy a condition and obtaining the size of the resulting array:
Block argument to Array#index, Array#rindex [Ruby2]
They can now take a block to make them work like #select.
Array#combination
yields all the combinations of length n of the elements in the array to the given block. If no block is passed, it returns an enumerator instead. The order of the combinations is unspecified.
Array#permutation
Array#pop, Array#shift
They can take an argument to specify how many objects to return:
Hash preserves order!
vs.
Numeric#upto, #downto, #times, #step
These methods return an enumerator if no block is given:
Range#cover?
compares value to the begin and end values of the range, returning true if it is comprised between them, honoring #exclude_end?.
Limit input in IO#gets, IO#readline, IO#readlines, IO#each_line, IO#lines, IO.foreach, IO.readlines, StringIO#gets, StringIO#readline, StringIO#each, StringIO#readlines
These methods accept an optional integer argument to specify the maximum amount of data to be read. The limit is specified either as the (optional) second argument, or by passing a single integer argument (i.e. the first argument is interpreted as the limit if it’s an integer, as a line separator otherwise).
IO#ungetc, StringIO#ungetc
Allows to push back an arbitrarily large character.
Seven predicate methods where added for the weekdays:
6 Jul
Time saving tips and tricks!
This first tip is something that I use almost daily. Do you ever want to change a filename to something that is similar to the original name? For instance, maybe you just want to change/add/remove the extension? Well, if you are using a reasonable shell you can do the following:
Or how about this; fairly often I will be programming and I will be adding a predefined string to the end of another string a bunch of times, except for the last time. The idea is to put the predefined string between some other things. This is pretty regular if you are generating HTML or SQL. Well, instead of doing the following:
you can do:
Another thing that I find myself doing often is the following:
That will generate the question marks for an SQL statement. Again, that’s a little messy and there is a cleaner way to do it.
Much better! It’s much shorter and should be easier to understand for other Ruby programmers.
It’s good to put things like this into practice, because it will make your code more readable and easier to maintain. Generally, in my manifesto, fewer lines of code (comments and whitespace don’t count) are better. Of course, in a language like Ruby this can create performance problems; it’s a balance between what works for you as the programmer and what works for the user. If the speed is really an issue, change the code. Otherwise, save your skull!
If you have any tips for regular things like this, let me know. I need to know stuff like this just as much as anyone else.