Prince:Ruby Best Practices

From OpenWetWare
Jump to navigationJump to search

Prince lab logo.png

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'