Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created February 8, 2012 22:06
Show Gist options
  • Save jcasimir/1774375 to your computer and use it in GitHub Desktop.
Save jcasimir/1774375 to your computer and use it in GitHub Desktop.

Fixnum

Concepts
  • Numbers in Ruby are Objects
    • We can call methods on numbers
    • We can define methods on numbers
  • Tricks with Numbers
    • Integer with Integer gives Integer
    • Integer with Float gives Float
    • Calling to_i on a String
  • Truth
    • 0 is truthy
  • Order of Operations
Essential Methods
  • Core Ruby
    • Core Math Operators (+, -, *, /)
    • Modulo %
    • times
    • to_s / to_f
  • With ActiveSupport
    • Date/Time Extensions: .hours, .days, .weeks, .months, .years, .ago/.until, .since/.from_now
    • Byte Extensions: .bytes, .kilobytes, .megabytes, .gigabytes
    • ordinalize

Float

Concepts
  • Challenges when using Floats
    • Math is not precise
    • Formatting can be more difficult
  • Alternatives
    • BigDecimal
    • Avoiding fractional components
  • Using Kernel.format
  • Whole Number Math vs. Fractional Math (ex: 1.0/2 vs 1/2)
Essential Methods
  • Core Ruby
    • Core Math Operators (+, -, *, /)
    • ceil
    • to_i / floor
    • to_s
@JoshCheek
Copy link

Maybe also:

  • integer division returns an integer (someone is likely to run into 1/2 # => 0)
  • operators with mixed integer / float return foat
  • 0 is truthy in conditionals
  • keep even spacing around operators (e.g. num1 *num2 is different from num1 * num2)
  • operator precedence follows mathematical precedence
  • operators are syntactic sugar for method invocation (IDK if you have a separate area where you're discussing operators)
  • possibly 123 vs 0123 vs 0x123 vs 0b101 (that might be out of scope)

@jcasimir
Copy link
Author

jcasimir commented Feb 9, 2012

Thanks @JoshCheek, added most of them!

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