Prince:Ruby Best Practices: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
(New page: {{PrinceLab}} [From Russ Olsen's Eloquent Ruby] The ruby community has settled on some conventions in an effort to create readable code. Following these conventions will help you to writ...)
 
No edit summary
 
Line 16: Line 16:
* if/unless & while/until are logical pairs: use the one that fits the occasion
* if/unless & while/until are logical pairs: use the one that fits the occasion
* use '''each''' instead of '''for'''
* use '''each''' instead of '''for'''
*
* use case when
* everything in ruby returns, so can use the return value
* use ternary operator:  is_alive? ? 'bark' : 'do_nothing'

Latest revision as of 09:23, 24 May 2011

Home        Lab Members         Research         Publications         Internal         Mass Spec         Contact        


[From Russ Olsen's Eloquent Ruby] The ruby community has settled on some conventions in an effort to create readable code. Following these conventions will help you to write clean code and allow others to understand it.

Chapter 1

  • readability/comprehensibility trumps all other conventions
  • Indent with 2 spaces (use spaces over tabs)
  • CamelCase classes, snake_case_everything_else, & CONSTANTS_UPPER_SNAKE
  • generally use parentheses for arguments (can sometimes leave off): mymethod(one, 3)
  • leave parentheses off empty argument lists: word.split
  • generally leave parentheses off control statements, so write: if something...
  • one line block: {}, multi-line block: do ...\n... end

Chapter 2

  • if/unless & while/until are logical pairs: use the one that fits the occasion
  • use each instead of for
  • use case when
  • everything in ruby returns, so can use the return value
  • use ternary operator: is_alive? ? 'bark' : 'do_nothing'