In Ruby, classes are cheap

In Ruby, classes are cheap

Developers coming from Perl or Php often bring conventions from those languages into Ruby. One important thing I like to tell new Ruby developers is that classes in Ruby are cheap so use them liberally. Ruby is an Object Oriented language and objects are the best way to communicate your intent to future developers reading your code. Let’s look at examples of where a class is more appropriate.

First, we have a method returning multiple values for status.

class ExternalOrderService
  def handle_order(order_status)
    # stuff here
    if order_status.include?('Fail on Shipment')
      message = 'Order Failed because of Shipment'
      success =  false
    elsif order_status.include?('Order Processed')
      message = 'Order Failed because of Shipment'
      success =  false
    else
      message = 'Order Failed for unknown reason'
      success =  false
    end
    [message, success]
  end
end

Read More

Multi line strings in Ruby

Multi line strings in Ruby

Ruby has some nice ways to multi line strings. First, this one is the worst. If you are coming from Java or Javascript you might be tempted use it though.

# Do not do it this way
str = 'Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr ' +
        'persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.' +
        ' Cum utinam nonumes nominavi eu, mazim dolor per in.' +
        "\n" +
        'Debet vivendo pri ei, nec hinc labore in. Duo ad vocibus oporteat ' +
        'appellantur. Nibh idque no eos, mel viris partiendo ei, te pro ' +
        'discere diceret. Vero aliquid quo an. Porro lobortis convenire vis ' +
        'ea, copiosae epicurei percipit nam ut.'

Read More