[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Lachie
I often duck punch this: class Hash def self.auto(&block) Hash.new do |h,k| h[k] = block.arity == 1 ? yield(k) : yield end end end so that: things_by_key = Hash.new{|h,k| h[k] = []} becomes things_by_key = Hash.auto { [] } You can even nest them without hurting yourself deep

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Andrew Grimm
I had a look at Acme::Don't while I was there, and it looks like an interesting poor person's multiline comment (but remember, commenting out code is bad mmmkay?) Andrew On Tue, Mar 10, 2009 at 4:35 PM, Julio Cesar Ody wrote: > > When I saw that class declaration getting a condition right at th

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Andrew Grimm
At one stage when I was playing around with a few implementations of XML parsing, I had something like class XmlParserImplementation1 end class XmlParserImplementation2 end XmlParser = XmlParserImplementation1 Andrew On Tue, Mar 10, 2009 at 4:31 PM, Cameron Barrie wrote: > conditionals on the

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Cameron Barrie
LOL, to true. C On 10/03/2009, at 4:35 PM, Julio Cesar Ody wrote: > > When I saw that class declaration getting a condition right at the > end, there's one thing that came to my mind: > > http://search.cpan.org/~pjf/Acme-ButFirst-1.00/lib/Acme/ButFirst.pm > > > > > On Tue, Mar 10, 2009 at 4:31

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Clifford Heath
On 10/03/2009, at 4:15 PM, Daniel N wrote: > One of the things I like with hashes is default values set with > procs :) > > things_by_key = Hash.new{|h,k| h[k] = []} > things.each do |thing| > things_by_key[thing.key] << thing > end > > I use these all the time for default values in all sorts

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Julio Cesar Ody
When I saw that class declaration getting a condition right at the end, there's one thing that came to my mind: http://search.cpan.org/~pjf/Acme-ButFirst-1.00/lib/Acme/ButFirst.pm On Tue, Mar 10, 2009 at 4:31 PM, Cameron Barrie wrote: > conditionals on the end of lines. simple but awesome. >

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Clifford Heath
Predicate-style naming is derived from LISP I believe, where it was common to use _p on the end in lieu of ?. At ManageSoft around 12:30, there were often exclamations of "lunch_p" or "food_p" :-). Predicates are mathematical functions, so it's natural to mathematicians to omit the "is_" in a la

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Cameron Barrie
conditionals on the end of lines. simple but awesome. puts "Awesome" if @apple.awesome? rather than the verbose and less readable C example :P if (apple->flavour == "awesome") printf("Awesome\n"); You can even use them on the ends of class definitions and such if really want to. class

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Steve Hayes
Unless you grew up using English I suppose. Aren't we trying to make our code more readable, rather than more terse? On 10/03/2009, at 4:14 PM, Pat Allan wrote: > > It's general ruby best-practices, I think. You don't need the 'is' > prefix, as the punctuation provides the same effect, and much

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Andrew Grimm
You're going from procedural to object-oriented coding there. That's usually fine, but what if you're doing something like a factory method if has_no_funny_characters?(title) create_object(title) else return nil end (assume you don't have activerecord and object.valid?) Andrew On Tue, Mar 1

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Daniel N
On Tue, Mar 10, 2009 at 4:05 PM, Clifford Heath wrote: > > On 10/03/2009, at 3:52 PM, Nick Partridge wrote: > > Symbol-to-proc, and the use of `&` to coerce things to procs: > > > > [1,2,3,4,5].select(&:odd?) > > > > Awww yeah. > > Cute but wasteful, and now deprecated in all Rails core code becau

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Pat Allan
It's general ruby best-practices, I think. You don't need the 'is' prefix, as the punctuation provides the same effect, and much more cleanly. -- Pat On 10/03/2009, at 4:12 PM, Torm3nt wrote: > Hi Tim, > > Is this just personal preference? > > I was using the method names to exaggerate the

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Torm3nt
Hi Tim, Is this just personal preference? I was using the method names to exaggerate the example =) But now that I know you hate it WELL... heh On Tue, Mar 10, 2009 at 4:09 PM, Tim Lucas wrote: > > > if is_it_good?('apple') > > return it_is_awesome! > > end > > Get rid of any "it_is" or

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Tim Lucas
> if is_it_good?('apple') > return it_is_awesome! > end Get rid of any "it_is" or "is" in boolean methods please. empty? not is_empty? So something like: def respond_to_bad_joke if @apple.good? eat @apple else throw @apple end end A real example would be much better. --

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Julio Cesar Ody
> Symbol to proc uses rails. Don't know which part of rails, or whether other > web frameworks will accept it, but it's not valid in all six states. http://www.infoq.com/news/2008/02/to_proc-currying-ruby19 --~--~-~--~~~---~--~~ You received this message because y

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Clifford Heath
On 10/03/2009, at 3:52 PM, Nick Partridge wrote: > Symbol-to-proc, and the use of `&` to coerce things to procs: > > [1,2,3,4,5].select(&:odd?) > > Awww yeah. Cute but wasteful, and now deprecated in all Rails core code because of the extra (non-gc-able?) object it creates. I like this for creat

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Andrew Grimm
Symbol to proc uses rails. Don't know which part of rails, or whether other web frameworks will accept it, but it's not valid in all six states. Andrew On Tue, Mar 10, 2009 at 3:52 PM, Nick Partridge wrote: > > Symbol-to-proc, and the use of `&` to coerce things to procs: > > [1,2,3,4,5].select

[rails-oceania] Re: Your favourite ruby idioms?

2009-03-09 Thread Nick Partridge
Symbol-to-proc, and the use of `&` to coerce things to procs: [1,2,3,4,5].select(&:odd?) Awww yeah. On Tue, Mar 10, 2009 at 3:39 PM, Torm3nt wrote: > We all know ruby can read very similarly to english at times, but also has > some very curious idioms around. Wanted to see what everyone's favo

[rails-oceania] Your favourite ruby idioms?

2009-03-09 Thread Torm3nt
We all know ruby can read very similarly to english at times, but also has some very curious idioms around. Wanted to see what everyone's favourite syntactical options were in ruby. I'll start: if is_it_good?('apple') return it_is_awesome! end ---

[rails-oceania] Re: Exporting content to Excel

2009-03-09 Thread Marc
I might be way off the mark here. But I did find if you don't require automating the export to excel and if you are using rails, you could just use a respond_to with a format.xls. You just have to specify the mime_type (Mime::Type.register "text/html", :xls), and in the respond_to block, do someth

[rails-oceania] Re: Exporting content to Excel

2009-03-09 Thread James Sadler
Hi Matthew, I can't help specifically with the Spreadsheet gem (I recall trying it and having similar issues and crashes), but there is a robust alternative. I've had to deal with Excel files from a Rails app recently, and I found the Ruby Excel libraries to be very under-featured or fragile. In

[rails-oceania] Re: Exporting content to Excel

2009-03-09 Thread Matthew Delves
Hi Kirk, The file gets written to the right location though it ends up only being about 8 bytes in length. I would guess from this that it doesn't write it correctly. Thanks, Matt Delves On Mar 9, 2009, at 9:53 PM, Torm3nt wrote: > Hi Matt, > > Could you be a little more specific? What do

[rails-oceania] Re: Exporting content to Excel

2009-03-09 Thread Torm3nt
Hi Matt, Could you be a little more specific? What do you mean by "write the file correctly"? 1. Doesn't write it at all? 2. Writes the file but screws up the formatting? 3. Writes it to the wrong location? .etc.etc. Kirk On Mon, Mar 9, 2009 at 9:04 PM, Matthew Delves wrote: > > Greetings all

[rails-oceania] Exporting content to Excel

2009-03-09 Thread Matthew Delves
Greetings all, I've been looking at the spreadsheet (http:// spreadsheet.rubyforge.org/) gem to get data exported from my database to an excel document, though am running into a couple of hitches with it. It doesn't want to write the file correctly. The code that I currently have is: --