#6
Mar 16, 2007

Shortcut Blocks with Symbol to_proc

It may have a goofy syntax, but the Symbol#to_proc feature Rails adds allows you to do simple blocks very quickly and easily.
Download (6.5 MB, 3:44)
alternative download for iPod & Apple TV (4.9 MB, 3:44)
# models/task.rb
def self.all_names
  find(:all).collect(&:name)
end

RSS Feed for Episode Comments 19 comments

1. Frank Jul 09, 2007 at 09:30

Maybe a stupid question but is it possible to use Symbol to_proc even for methods which need an argument?


2. Ryan Bates Jul 09, 2007 at 09:32

Nope, it only works on very simple method calls which don't take an argument. Anything more complicated and you will need to use the full block.


3. Mike Stramba Jul 11, 2007 at 11:27

Ryan,

Can more than one attribute be combined / concatenated ?

e.g. :
Something like ... (this doesn't work) :

ActorNames = Actor.find(:all).collect(&:last_name + ' ' + &:first_name)

.. neither does :

ActorNames = Actor.find(:all).collect(&:last_name,&:first_name)

Mike


4. Ryan Bates Jul 11, 2007 at 12:10

@Mike, nope, you can only call a single method with Symbol to_proc. If you want to combine two methods like you're trying to do, I recommend creating a new "full_name" method in the actor model to do this. That way you're back to calling one method.


5. Bobby Digital Jul 25, 2007 at 00:53

Before your readers subscribe to this method, they should be aware of the performance issue.

http://m.onkey.org/2007/6/30/let-s-start-with-wtf

Apparently, nothing beats a good olf-fashioned block


6. Allen Kim May 12, 2009 at 06:48

Two ways to do :

1.find(:all).map{|t| t.name}
2.find(:all).collect(&:name)


7. acheter Dofus Kamas Nov 08, 2009 at 19:55

good post!


8. comprar Metin2 Yang Nov 08, 2009 at 19:56

wonderful!


9. Ira Feb 15, 2010 at 14:21

Its amazing interesting bangs!


10. Pavlusa Feb 22, 2010 at 12:14

Symbol to_proc. If you want to combine two methods like you're trying to do, I recommend creating a new "full_name" method in the actor model to do this.


11. Lyudmilich Feb 23, 2010 at 09:48

super !!!


12. Lyudusha Mar 17, 2010 at 13:42

[url=http://nbparts.ru/]Lyudusha[/url]
good post!


13. christian louboutin heels Jun 14, 2010 at 03:05

well, here is my comment are u happy?


14. Christian Louboutin Slingback Jun 24, 2010 at 02:21

Keep it short and simple. This is what you do!


15. ed hardyswimwear Jul 20, 2010 at 00:22

http://www.pandora-jewelry-club.com/


16. iphone case Jul 21, 2010 at 23:49

Your do have some unique ideas here and I expect more articles from you.


17. moncler down coats Jul 22, 2010 at 02:38

We are selling all kinds of moncler jackets,moncler coats,moncler vests,moncler shirts and so on,all of our products are at high quality and low price,and they are free shipping.


18. chanel boots Jul 23, 2010 at 01:39

best shoes.


19. pandora jewelry Jul 23, 2010 at 20:00

Would you want to share some fashionable


20. chanel bags Jul 23, 2010 at 20:01

Do you want to be a stylish people


21. tiffany notes Jul 30, 2010 at 02:24

Great site. This could probably have the refactoring tag added t it.


22. tiffany notes Jul 30, 2010 at 02:24

Great site. This could probably have the refactoring tag added t it.


24. mountain bike Aug 09, 2010 at 18:28

It's a very meaningful activity. Looking forward to joining you.


25. paper tissue Aug 09, 2010 at 18:30

Very interesting and useful information! It's pity that such occasions are still exist in the world, but we should act all together for to prevent them. Thanks.


25. digital blood pressure monitor Aug 09, 2010 at 18:32

thanks for the tips,very useful. Thanks for sharing it.


26. basal thermometer Aug 09, 2010 at 18:34

Well, it is very useful information shared. Thanks a lots.


27. glue gun Aug 09, 2010 at 18:36

Thanks for putting this in a clear concise format. I appreciate it!


28. solar collector Aug 09, 2010 at 18:38

I really appreciate what you post.


29. website design Aug 09, 2010 at 23:35

to_proc Symbol. If you want to combine two methods that you try to do, I recommend you create a new "FULL_NAME" model of the actor method to do this.


30. free directory list Aug 11, 2010 at 22:15

WOW, this is great!


31. uggs online Aug 13, 2010 at 22:11

Article is very nicely written.


32. Black Agates Beads Aug 14, 2010 at 21:01

It is the tears of the earth that keep here smiles in bloom.


33. milgaussreplica Aug 16, 2010 at 01:32

comes housed in a black plastic case with stainless steel lugs at each side. The black
wristband is nicely crafted from leather, and in case you plan on banging your new watch
around a bit, the display is protected by a durable, scratch-resistant mineral crystal
face.


34. wariror Aug 19, 2010 at 13:40

hello thank you good information


35. WoW Accounts Aug 20, 2010 at 21:11

Hi Ryan -
Great stuff, but be careful.
Its lucky all your passwords are 'secret'.
You set up a table in the Outline, but you use literal text in the steps!


36. wedding dance Aug 23, 2010 at 19:45

Your blog keeps getting better and better! Your older articles are not as good as newer ones, you have a lot more creativity and originality. Now keep it up!


37. first time home buyer Aug 24, 2010 at 08:58

Wow. This blog is truly a gold mine. I have been wallowing on this for quite sometime and here I am reading about it! I will actually try these tips and let you know how they work out! Thanks again mate.


38. All Nursing Programs Aug 24, 2010 at 23:06

Thank you for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such great information being shared freely out there.


39. SEO Webdesign Aug 26, 2010 at 04:04

Hello admin, good day. Excellent post. You have gained a new subscriber. Pls keep it up and I look forward to more of your cool articles. Sincerely, Chris.


40. louis vuitton shoes Aug 26, 2010 at 20:51

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


41. snow boots Aug 31, 2010 at 02:13

you can only call a single method with Symbol to_proc.


42. Dolce & Gabbana Belts Sep 01, 2010 at 22:54

I think this is a great post. One thing that I find the most helpful is number five. Sometimes when I write, I just let the flow of the words and information come out so much that I loose the purpose. It’s only after editing when I realize what I’ve done. There’s defiantly a lot of great tips here I’m going to try to be more aware of.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source