Sunday, November 25, 2007

Ruby Shorthand

Justin.tv's web code is written in Ruby. I've found myself using the same collection idioms over and over, so I've abstracted several of them into a file called shorthand.rb.



Most of our web code consists of manipulating collections in one form or another, which is probably why all the shorthand methods are for that. I'm particular proud of my % operator, which I believe is a specialized mapcar in spirit. Without further ado, code!




module Enumerable
def %(field)
map {|o| o.send(field)}
end
end

class Hash
def keys_sorted_by_value(options = Hash.new, &block)
limit = options[:limit] || size
offset = options[:offset] || 0

sorted = map.sort_by do |key, value|
if block
yield(key, value)
else
value
end
end

sorted.reverse! if options[:reverse]

sorted.map {|key, value| key}.slice(offset, limit) || Array.new
end

def hmap
h = dup
keys.each do |k|
h[k] = yield h[k]
end
h
end

def to_params
map {|k, v| "#{k}=#{CGI::escape v.to_s}"}.join("&")
end
end


class Array
def hash_by(field)
h = Hash.new
each {|o| h[o.send(field)] = o}
h
end

def center_first
centered = Array.new
last_pushed = false
each do |x|
centered.push(x) unless last_pushed
centered.unshift(x) if last_pushed
last_pushed = !last_pushed
end
centered
end

def random_subset(n=size)
shuffle.slice(0, n)
end

def shuffle
sort_by { rand }
end

def divide
evens = Array.new
odds = Array.new
each_with_index do |x, i|
if i % 2 == 0
evens << x
else
odds << x
end
end
[evens, odds]
end

def remove!(item)
reject! {|x| x == item}
end

def remove(item)
reject {|x| x == item}
end
end


Other coders: If you have your own idioms like these, I'd love to see them! Post them!

20 comments:

M-toto said...

Excellent post. 토토사이트
I was checking constantly this blog and I'm impressed! 토토사이트추천

Very helpful info particularly the last part :) I care for such info much.
I was seeking this particular info for a long time. Thank
you and good luck. 먹튀검증

M-toto said...

Hi there very cool web site!! Guy .. Excellent .. 온라인카지노

Superb .. I will bookmark your web site and take 온라인카지노사이트

the feeds additionally? I'm glad to search out a
lot of useful info hede wituin thhe submit, we'd like work oout more strategies 카지노

토토 said...

Excellent article. I certainly appreciate this website.
Stick with it!


토토사이트
스포츠토토티비
토토

토토 said...

Everyone loves it when people come together
and share opinions. Great site, continue the good work!

토토
배트맨토토
배트맨토토프로

sportstotohot.com said...

You made some really good points there. I looked on the net for more information about the issue and found most people will go along with your views on this site. 스포츠토토

racesite.pro said...

This is a fascinating subject and I hope to learn more about it in the future. 온라인경마

reelgame.site said...

Great article post.Really looking forward to read more. 릴게임

totosite365.info said...

Thanks for the post. I’ll definitely comeback. 스포츠토토

Majortotositepro1 said...

Finally a simple to understand information, appreciated!

Also visit my blog:

토토사이트
경마사이트
바카라
토토사이트

Majortotositepro1 said...

It’s nearly impossible to find well-informed people in this particular topic,
but you sound like you know what you’re talking about!

majortotositepro1
oncasinositenet1
totopickpro1
majortotositepro1

sportstototopcom said...

This is a wonderful article, I really appreciate your efforts and I will be waiting for your next post thank you once again. 스포츠토토

gostopsite said...

Your choice of writing is really amazing.
카지노사이트

casinositewiki 카지노사이트 said...

I am really impressed with this blog article, Keep it up!
카지노사이트위키
온라인카지노사이트
카지노사이트탑
바카라사이트

casinositeking 카지노사이트 said...

You put very helpful information. Keep it up. Keep blogging.
카지노사이트
온라인카지노
카지노
바카라

sportstotolink said...

When I read your article on this topic, the first thought seems profound and difficult.
토토
먹튀검증

casinositezone said...

Your post is very interesting to me. Reading was so much fun.
카지노사이트
바카라사이트

safecasinositenet said...

I think the reason reading is fun is because it is a post related to that I am interested in.
바카라사이트
온라인카지노

casinositenetcom said...

It’s very straightforward to find out any topic on net as compared to books, as I found this article at this site
온라인카지노
카지노

RACESITE said...

😳👤 gⓞØ𝐝 𝒹Δ𝕐 нⒺ尺ᗴ, 𝓲'Ⓜ ђέŘє ţ𝓸 Rᗴ𝐀∂ 𝔂ㄖย𝕣 ν𝐞Řⓨ 𝓝Ɨ𝓒𝔢 α𝓡tĮc𝕃E, ᵗᕼ𝓘𝓢 卂Ř𝐭เC𝐋𝑒 𝔴as Ѷ乇𝐑у 𝐓𝔯υt𝓗𝐅𝓤𝓛. 🐉✊
경마사이트

baccaratsite said...

like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed…

바카라사이트
카지노사이트