question?

taryneast May 7, 2009

Shouldn't the second example be:

[1,2].zip(a,b)         #=> [[1, 4, 7], [2, 5, 8], [nil,6,9]]

??? or am I missing something?

clarification

brian May 4, 2009 1 thank

Via Kenneth Kalmer:

From the man page: If salt is a character string starting with the characters “$id$” followed by a string terminated by “$”: $id$salt$encrypted then instead of using the DES machine, id identifies the encryption method used and this then determines how the rest of the password s...

Create a Hash from two Arrays

mindloaf May 1, 2009 8 thanks

Here is my favorite idiom for creating a Hash from an Array of keys and an Array of values:

keys = [:a, :b]
values = [1,2]
h = Hash[*keys.zip(values).flatten]      # => {:b=>2, :a=>1}

Test if one array includes the elements of another

mindloaf May 1, 2009 4 thanks

You can just use a set difference (aka minus) to see if one array includes all elements of another

not_included = [1,2,3] - (1..9).to_a
not_included      # => []


not_included = [1,2,3,'A'] - (1..9).to_a
not_included      # => ["A"]

Use intersection to test if any of the one are...

Re: Find random record

RISCfuture May 1, 2009 2 thanks

Ordering by RAND() is not a wise idea when you have a large table with lots of rows. Your database will have to calculate a different random value for every row in your database -- O(N) -- then sort the entire table by those values -- O(N log N).

There ar...

For specific entries, use Dir.glob

tadman Apr 30, 2009 1 thank

When working with the contents of a directory it's not uncommon to be interested in a specific subset of the entries present.

Dir.glob can be used to fetch entries by name and File.stat can be used to determine the type of file.