Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created May 8, 2012 15:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahoward/2636430 to your computer and use it in GitHub Desktop.
Save ahoward/2636430 to your computer and use it in GitHub Desktop.
style is the only thing.
# shitty
attributes[:name] = options[:name] unless options[:name].blank?
attributes[:first_name] = options[:first_name] unless options[:first_name].blank?
attributes[:last_name] = options[:last_name] unless options[:last_name].blank?
attributes[:linkedin_id] = options[:linkedin_id] unless options[:linkedin_id].blank?
attributes[:company] = options[:company] unless options[:company].blank?
attributes[:company_id] = options[:company_id] unless options[:company_id].blank?
attributes[:linkedin_picture_url] = options[:linkedin_picture_url] unless options[:linkedin_picture_url].blank?
# shitty, but i can read it...
attributes[:name] = options[:name] unless options[:name].blank?
attributes[:first_name] = options[:first_name] unless options[:first_name].blank?
attributes[:last_name] = options[:last_name] unless options[:last_name].blank?
attributes[:linkedin_id] = options[:linkedin_id] unless options[:linkedin_id].blank?
attributes[:company] = options[:company] unless options[:company].blank?
attributes[:company_id] = options[:company_id] unless options[:company_id].blank?
attributes[:linkedin_picture_url] = options[:linkedin_picture_url] unless options[:linkedin_picture_url].blank?
# at least it's DRY, but fuck it's hard to read...
[:name, :first_name, :last_name, :linkedin_id, :company, :company_id, :linkedin_picture_url].each do |key|
attributes[key] = options[key] unless options[key].blank?
end
# minimum required to be good
#
# * the variable name makes the intention clear
# * the list isn't jammed onto one line hiding typos and confounding quick editing
# * it's primed for configuration (whitelist is an option)
# * stack errors come from *one line*
# * it separates the logic of listing what to whitelist and how to extract a key from options
# * reading a commit diff is actually possible (thanks @drbrain)
#
whitelist = [
:name,
:first_name,
:last_name,
:linkedin_id,
:company,
:company_id,
:linkedin_picture_url
]
whitelist.each do |key|
attributes[key] = options[key] unless options[key].blank?
end
@steveklabnik
Copy link

👍

@ahoward
Copy link
Author

ahoward commented May 8, 2012

@drbrain pointed out that an additional advantage is that the last example diffs well in a commit

@rab
Copy link

rab commented May 9, 2012

I will also point out that the Ruby parser allows a comma after the last item in an Array literal (or a Hash) such as:

    whitelist = [ 
      :name,
      :first_name, 
      :last_name,
      :linkedin_id,
      :company,
      :company_id,
      :linkedin_picture_url,
    ]

This has the advantage of having a single-line diff when the next whitelist item is added. The alternative is to have a patch like:

       :company_id,
-      :linkedin_picture_url
+      :linkedin_picture_url,
+      :pet_name
     ]

Rather than simply:

       :linkedin_picture_url,
+      :pet_name,
     ]

@ahoward
Copy link
Author

ahoward commented May 9, 2012

i am a huge fan of 'trailing comma' for that very reason. however, years ago matz threatened to make it go away in 1.9.ish.... can anyone confirm otherwise so as to advocate resumed usage?

@rab
Copy link

rab commented May 9, 2012

Well, for Hash at least, the Ruby Spec project codifies the "hanging comma":

https://github.com/rubyspec/rubyspec/blob/master/language/hash_spec.rb#L48

And similar specs for a hanging comma at the end of an argument list for send:

https://github.com/rubyspec/rubyspec/blob/master/language/versions/send_1.9.rb

I don't see anything equivalent for Array unfortunately. However, there does seem to be support in the grammar parse.y for it:

(excerpted from four locations in the file)

primary     : literal
        | tLBRACK aref_args ']'
            {
            /*%%%*/
            if ($2 == 0) {
                $$ = NEW_ZARRAY(); /* zero length array*/
            }
            else {
                $$ = $2;
            }
            /*%
            $$ = dispatch1(array, escape_Qundef($2));
            %*/
            }
aref_args   : none
        | args trailer
            {
            $$ = $1;
            }
        | args ',' assocs trailer
            {
            /*%%%*/
            $$ = arg_append($1, NEW_HASH($3));
            /*%
            $$ = arg_add_assocs($1, $3);
            %*/
            }
        | assocs trailer
            {
            /*%%%*/
            $$ = NEW_LIST(NEW_HASH($1));
            /*%
            $$ = arg_add_assocs(arg_new(), $1);
            %*/
            }
        ;

trailer     : /* none */
        | '\n'
        | ','
        ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment