Nice! :-) 2009/3/23 David Lee <[email protected]>
> eh sorry for flogging this but i added assignment in case anyone's > interested > http://gist.github.com/83473 > > > On Mon, Mar 23, 2009 at 7:19 PM, David Lee > <[email protected]>wrote: > >> actually, a bit of tweaking and that's a passable ordered hash for many >> purposes: >> >> module OrderedHash >> def []( index ) >> begin >> super( index ) >> rescue TypeError >> self.detect do |i| >> i.first == index >> end[1] # if index.class ... >> end >> end >> >> def keys >> map(&:first) >> end >> >> def values >> map(&:last) >> end >> end # ArrayNameAccessor >> >> h = [[:a,1],['b',2],[:c,3]].extend OrderedHash >> h.keys # => [:a, 'b', :c] >> h[0] # => [:a, 1] >> h.values[0] # => 1 >> h[:a] # => 1 >> >> >> On Mon, Mar 23, 2009 at 5:46 PM, David Lee >> <[email protected]>wrote: >> >>> I'm a slut for extending arrays / hashes with little modules. >>> >>> An example from a project I'm working on: >>> >>> module ArraySmartIndex >>> def []( index ) >>> begin >>> super( index ) >>> rescue TypeError >>> self.detect do |i| >>> i.name == index.to_sym >>> end if index.respond_to?(:to_sym) >>> end >>> end >>> end # ArrayNameAccessor >>> >>> [].extend( ArraySmartIndex ) >>> >>> ^ if you pass a symbol as an array index, rescue it and treat the array >>> like a hash keyed on it's member's name attribute :) >>> >>> shameless plug: http://github.com/davidlee/zen-koan/tree/master >>> >>> >>> On Wed, Mar 11, 2009 at 9:48 PM, Andrew Grimm >>> <[email protected]>wrote: >>> >>>> Are you suggesting using the variable named defaults in the rest of the >>>> method, rather than the variable named options? >>>> >>>> Andrew >>>> >>>> >>>> On Wed, Mar 11, 2009 at 9:43 PM, Torm3nt <[email protected]> wrote: >>>> >>>>> Unless I'm misunderstanding it, I'm pretty sure it sets defaults with >>>>> the new options, without you having to do: defaults = >>>>> defaults.merge(options) >>>>> >>>>> >>>>> On Wed, Mar 11, 2009 at 9:39 PM, Andrew Grimm < >>>>> [email protected]> wrote: >>>>> >>>>>> Wouldn't that modify defaults, not options? >>>>>> >>>>>> Andrew >>>>>> >>>>>> >>>>>> On Wed, Mar 11, 2009 at 8:46 PM, Torm3nt <[email protected]> wrote: >>>>>> >>>>>>> Can't you do defaults.merge!(options) to modify the object in place? >>>>>>> Kirk >>>>>>> >>>>>>> >>>>>>> On Wed, Mar 11, 2009 at 5:33 PM, Andrew Grimm < >>>>>>> [email protected]> wrote: >>>>>>> >>>>>>>> This is probably personal preference, but I usually use an >>>>>>>> explaining variable of defaults and have >>>>>>>> >>>>>>>> defaults = {:width => 800, :align => :left_justify} >>>>>>>> options = defaults.merge(options) >>>>>>>> >>>>>>>> This doesn't mutate the existing options object but creates a new >>>>>>>> object. I'm not sure if this is a bug or feature. >>>>>>>> >>>>>>>> I use double quotes rather than single, but that's because I use >>>>>>>> plain command line ruby rather than rails. >>>>>>>> >>>>>>>> Andrew >>>>>>>> >>>>>>>> >>>>>>>> On Wed, Mar 11, 2009 at 4:03 PM, David Phillips < >>>>>>>> [email protected]> wrote: >>>>>>>> >>>>>>>>> >>>>>>>>> On 11/03/2009, at 11:20 AM, Christopher Robbie wrote: >>>>>>>>> >>>>>>>>> > c = a || b >>>>>>>>> > >>>>>>>>> > if 'a' is got something, use it, if 'a' is nil(or false) then use >>>>>>>>> 'b'. >>>>>>>>> > love it. >>>>>>>>> >>>>>>>>> c ||= a >>>>>>>>> >>>>>>>>> c = a, but only if c is nil >>>>>>>>> >>>>>>>>> def do_something options >>>>>>>>> options[:width] ||= 800 >>>>>>>>> options[:align] ||= :left_justify >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>> >>>>>> >>>>>> >>>>> >>>>> >>>>> >>>> >>>> >>>> >>> >>> >>> -- >>> cheers, >>> David Lee >>> >> >> >> >> -- >> cheers, >> David Lee >> > > > > -- > cheers, > David Lee > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en -~----------~----~----~----~------~----~------~--~---
