|
Sunday, 21 August 2011 13:55 |
|
Setting Up Network RAID1 With DRBD On Debian Squeeze
This tutorial shows how to set up network RAID1 with the help of DRBD on two Debian Squeeze systems. DRBD stands for Distributed Replicated Block Device and allows you to mirror block devices over a network. This is useful for high-availability setups (like a HA NFS server) because if one node fails, all data is still available from the other node. Read more: |
|
|
Monday, 05 July 2010 18:45 |
Source: rbJL.net · 27 · New Array and Enumerable methods in Ruby 1.9.2: keep_if, chunk... [rbjl.net]
select! and keep_if
The select method (only choose the elements for which the block evaluates to true) got a mutator version select!. It does almost the same like the new keep_if method – with one subtle difference: select! returns nil if no changes were made, keep_if always returns the object.
combination and permutation
Ruby 1.9 introduced the useful methods combination and permutation. Now there are also repeated_combination and repeated_permutation:
[1,2,3].combination(2).to_a # also works in 1.9.1
#=> [[1, 2], [1, 3], [2, 3]]
[1,2,3].repeated_combination(2).to_a
#=> [[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]
[1,2,3].permutation(2).to_a # also works in 1.9.1
#=> [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
[1,2,3].repeated_permutation(2).to_a
#=> [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
rotate / rotate!
This method removes the first element and appends it. You can pass an integer, how many steps it should “cycle” (negative values are possible).
[1,2,3].rotate(2)
#=> [3, 1, 2]
[1,2,3].rotate(3)
#=> [1, 2, 3]
[1,2,3].rotate(-1)
#=> [3, 1, 2]
small changes: sort_by, uniq / uniq!, product
* sort_by (the block defines, how the Array should be sorted) also got a mutator version: sort_by!.
* product (combine all elements from both Arrays) now also accepts a block which yields every result, instead of returning it.
* uniq (remove duplicates) can now take a block, example from the docs:
c = [ "a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl" ]
c.uniq {|s| s[/^\w+/]} #=> [ "a:def", "b:abc", "c:jkl"
#=> ["a:def", "b:abc", "c:jkl"]
flat_map and its alias collect_concat
This method works like map, but if an element is an Enumerable itself, the applied block is also run for each of its child elements (but not recursively).
[[1,2],[3,[4,5]]].flat_map{|i|i} #=> [1, 2, 3, [4,5]]
I do not know (yet), if this is useful (and if it wouldn’t be cooler if it did something like .flatten.map), but we will see…
chunk
This one is interesting, but it is a little bit strange to use. It splits self into multiple Enumerators, using the rule given in the block. It keeps together those parts that “match” in series. It passes the result of the “filter” rule and an Enumerator of the successive elements:
(1..42).chunk{|n| n%11 == 0}.each{|result, elements|
puts "#{result}: #{elements*', '}"
}
output:
false: 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10
true: 11
false: 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21
true: 22
false: 23 - 24 - 25 - 26 - 27 - 28 - 29 - 30 - 31 - 32
true: 33
false: 34 - 35 - 36 - 37 - 38 - 39 - 40 - 41 - 42
slice_before
This method also lets you split the Enumerable. You simply specify a pattern/a block which has to match/be true and it splits before that element:
%w|Ruby is 2 parts Perl, 1 part Python, and 1 part Smalltalk|.slice_before(/\d/).to_a
#=> [["Ruby", "is"], ["2", "parts", "Perl,"], ["1", "part", "Python,", "and"], ["1", "part", "Smalltalk"]]
 Read more: |
|