Sorting an Array of Hashes with unknown keys

Jul 19
2008
AoH = [{"z"=>26}, {"g"=>7}, {"r"=>18}, {"a"=>1}, {"v"=>22}]
 
# sort by key
AoH.map{|e| e.to_a.flatten}.sort{|x,y| x[0] < => y[0]}
# sort by value
AoH.map{|e| e.to_a.flatten}.sort{|x,y| x[1] < => y[1]}

to truncate or not?

Jul 10
2008
# RoR source code:
# File vendor/rails/actionpack/lib/action_view/helpers/text_helper.rb, line 34
def truncate(text, length = 30, truncate_string = "...")
  if text.nil? then return end
  l = length - truncate_string.chars.length
  text.chars.length > length ? text.chars[0...l] + truncate_string : text
end
 
# My point of view:
def truncate(text, _length = 30, truncate_string = "...")
  text[0..(_length-1)] + truncate_string
end

Enigma

Jul 09
2008

Here’s the mirror
Behind there is a screen
Turn around
On both ways you can get in
The experience of survival
Look around just people, can you hear their voice
Find the one who’ll guide you to the limits of your choice
The experience / of survival
Try to think about it …
That’s the chance to live your life and discover
Try to think about it
The experience of survival
The experience …
The experience of survival
The experience …

TRAC – change SVN repository

Jul 07
2008

Go to your TRAC project dir and issue the following commands:

1. cd to_your_trac_project_dir

2. ruby -i -pe ’$_.sub! /(repository_dir\s=\s).+/, “repository_dir = NEW_repository_PATH”’ trac.ini

3. trac-admin . resync

regexp on String object

Jul 06
2008

Some of the more rarely used string methods:

str = "the tester"
 
str["tester"] #true, the String contains "tester"
str[/\w+\s\w+/] #true, the String matches the regexp

emulates capitalize!

str[/(\w+)/] = ($1[0] - 32).chr

RDoc Documentation

Google calculator response to “once in a blue moon”

Jul 04
2008

Results in the following:

once in a blue moon = 1.16699016 × 10-8 hertz

Can be tested here

Calendar

July 2008
M T W T F S S
« Jun   Aug »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tags