Favorites

May 23rd, 2013

Sometimes, I wanna set more than one var at once. This is one of my very favorite ruby tricks that I use every day:


irb(main):007:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):008:0> x, y, z = a.collect{|i| i * 2 }
=> [2, 4, 6]
irb(main):009:0> x
=> 2
irb(main):010:0> y
=> 4
irb(main):011:0> z
=> 6

Entry Filed under: Professional,Ruby on Rails

1 Comment Add your own

  • 1. steve  |  February 5th, 2014 at 12:35 pm

    Thank you for these code examples which are set out easily especially for a beginner like me. IKeep up the good work!

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Pages

Tweets

Today is my last day at the MBTA. So proud to have served Boston alongside my amazing team at CTD. I’m joining Indigo Ag to help farmers sustainably feed the planet, but before I go I wrote up a thing about what we’ve done on MBTA.com. bit.ly/2kjr15O pic.twitter.com/r4acHkz78j

About 3 months ago from Liana Leahy's Twitter via Twitter Web App

Meta