Quantcast

Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Wednesday, August 22, 2007

Really Useful Ruby Mysql One Liner

Mysql::Result.send(:include, Enumerable)
This one line makes interfacing with mysql results SO much easier.
require "mysql"
my = Mysql::new("host", "user", "passwd", "db")
res = my.query("select * from tbl")
res.each do |row|
col1 = row[0]
col2 = row[1]
end
This is from the standard documentation for the Ruby Mysql lib. What does it do? It grabs all items from tbl, and sets col1 and col2 to the first and second column of the LAST value of the result set. Not very useful unless you add a LIMIT 1 to your SQL statement. Much more useful would be this modified code.
require "mysql"
Mysql::Result.send(:include, Enumerable)
my = Mysql::new("host", "user", "passwd", "db")
res = my.query("select first_name, last_name from users")
full_names = res.map do |row|
"#{row[0]} #{
row[1]}"
end
Here we select all users from the table and end up with an array of full name strings. How did including Enumerable help us at all? It defined the map function for us, which natively does not exists for Mysql::Result objects. Why Enumerable is not included in the Mysql::Result class is a mystery to me.

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!

9 Comments:

Blogger Zhenhong Bao said...

longchamp handbags
coach outlet
true religion canada
oakley sunglasses
mulberry handbags
reebok shoes
burberry outlet
ralph lauren outlet
ralph lauren polo
ralph lauren
hermes belt for sale
versace sunglasses on sale
fitflops clearance
nike air max 90
thomas sabo outlet
pandora outlet
tory burch outlet
coach outlet
kobe shoes
oakley sunglasses
ghd hair straighteners
ray ban sunglasses
coach outlet online
tory burch shoes
coach outlet
ralph lauren outlet
mulberry handbags
michael kors outlet online
discount oakley sunglasses
burberry sunglasses on sale
ralph lauren pas cher
replica watches
oakley sunglasses
chrome hearts outlet online
longchamp pliage
20160531zhenhong

6:20 PM, May 30, 2016

 
Blogger Zhenhong Bao said...

toms outlet
michael kors online
ralph lauren uk
coach factory outlet
tory burch outlet
replica watches
toms shoes
fitflops sale
coach outlet
michael kors factory outlet
oakley sunglasses wholesale
longchamp handbags
air max 90
michael kors handbags wholesale
louis vuitton sunglasses
michael kors outlet online
louis vuitton bags
oakley sunglasses
ray ban sunglasses
fitflops clearance
chaussure louboutin
swarovski crystal
michael kors outlet store
michael kors outlet sale
chrome hearts outlet
louboutin pas cher
coach outlet online
louis vuitton outlet
louis vuitton neverfull sale
ralph lauren outlet
rolex watches
swarovski outlet
nike huarache
oakley sunglasses
ralph lauren outlet
20160531zhenhong

6:22 PM, May 30, 2016

 
Blogger Zhenhong Bao said...

toms outlet
tiffany jewellery
beats headphones
oakley sunglasses wholesale
ray-ban sunglasses
oakley sunglasses wholesale
michael kors uk
rolex watches
gucci sunglasses uk
michael kors outlet store
michael kors outlet
fitflops sale clearance
prada sunglasses for women
michael kors outlet
dior sunglasses
rolex uk
ralph lauren outlet
michael kors outlet
lululemon outlet
longchamp pas cher
camisetas futbol baratas
ferragamo outlet
air force 1 shoes
louis vuitton sunglasses for women
police sunglasses
futbol baratas
reebok shoes
louis vuitton handbags outlet
cartier watches
cheap nba jerseys
prada handbags
versace sunglasses
polo ralph lauren
jordan pas cher
ray-ban sunglasses
20160531zhenhong

6:27 PM, May 30, 2016

 
Blogger xjd7410@gmail.com said...

christian louboutin shoes
gucci outlet
ray ban outlet
michael kors handbags
oakley sunglasses
toms
nike air max
nike air max
michael kors outlet online sale
nike outlet
nike huarache shoes
christian louboutin outlet
basketball shoes
ghd hair straighteners
michael kors outlet online sale
nike blazers
louis vuitton handbags
coach outlet store online
adidas shoes
oakley sunglasses
jordan 8s
louis vuitton
rolex submariner
louis vuitton
mont blanc pens
christian louboutin shoes
michael kors handbags
christian louboutin sale
christian louboutin outlet
louboutin pas cher
ray ban sunglasses
kobe bryant shoes
adidas ultra boost
timberland outlet
polo ralph lauren outlet
nike outlet store
nfl jerseys wholesale
adidas uk
hollister clothing store
nike air huarache
2016.7.7haungqin

12:32 AM, July 07, 2016

 
Blogger Network Esquire said...

Really unbelievable, is not as easy as you can see, the posts difficulty and challenge waiting for you and that's the main difficulties that make us interesting.
Friv, Y8, Y3, Frozen Games

6:10 PM, July 16, 2016

 
Blogger Hua Cai said...

fitflops sale
ugg outlet
mulberry handbags sale
true religion outlet
michael kors outlet clearance
true religion jeans
new balance outlet
michael kors outlet clearance
ferragamo outlet
christian louboutin shoes
links of london
michael kors wholesale
tory burch outlet online
prada sneakers
true religion outlet
swarovski outlet
jordan pas cher
nike air force 1
calvin klein underwear
louis vuitton pas cher
true religion jeans
ferragamo outlet
ferragamo shoes sale
louis vuitton outlet store
tiffany and co
coach outlet store
gucci outlet
louis vuitton outlet
oakley sunglasses uk
true religion jeans
michael kors outlet online
mulberry outlet
nike free 5
celine outlet online
louis vuitton bags
20160804caihuali

10:43 PM, August 03, 2016

 
Blogger ane semprul said...

This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
obat aborsi
cara menggugurkan kandungan
obat telat datang bulan
obat penggugur kandungan

9:01 PM, August 27, 2016

 
Blogger Fangyaya said...

abercrombie and fitch
tod's shoes
ugg boots uk
jordan 13
michael kors handbags
ugg outlet
uggs uk
toms shoes
uggs
michael kors outlet
canada goose jackets
coach outlet store online clearances
michael kors outlet clearance
louis vuitton outlet
canada goose outlet
michael kors handbags
oakley vault
louis vuitton handbags
ugg boots
michael kors outlet
the north face jackets
gucci outlet
louis vuitton outlet
louis vuitton outlet stores
michael kors handbags
celine handbags
christian louboutin outlet
true religion outlet
adidas nmd
chenyingying20160908

6:56 PM, September 07, 2016

 
Blogger Fangyaya said...

beats outlet
abercrombie & fitch
toms shoes
michael kors handbags
ugg outlet
coach outlet store online
toms shoes
nike outlet
cheap uggs
louis vuitton outlet
kate spade handbags
coach factory outlet
michael kors handbags
nike tn pas cher
ugg boots
adidas uk
insanity workout
canada goose coats
gucci belts
coach factory outlet
cincinnati bengals jerseys
louis vuitton
louis vuitton
chicago bulls jerseys
giuseppe zanotti sandals
gucci uk
uggs outlet
uggs outlet
adidas ultra boost
chenyingying20160908

7:18 PM, September 07, 2016

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

If you like this blog, you might also like top photography schools.