Skip to content

Instantly share code, notes, and snippets.

@lmduc
Last active April 22, 2019 11:48
Show Gist options
  • Save lmduc/2c72d96989c509cfbad3890299caa7aa to your computer and use it in GitHub Desktop.
Save lmduc/2c72d96989c509cfbad3890299caa7aa to your computer and use it in GitHub Desktop.

Ruby in a nut shell

String and symbol

String: 'hello world' or "hello world"

Symbol: :hello or :'hello world'

Differences:

  1. Symbols are initialized once and are stored in a hash table
a = 'hello'
b = 'hello'
a.object_id == b.object_id #=> false

a = :hello
b = :hello
a.object_id == b.object_id #=> true

Rails in a nutshell

How to handle exceptions gracefully

  1. Exceptions hierarchy

  1. Always rescue StandardError. If we rescue Exception, there might be a case we rescue a system exception such as NoMemoryError, or even worse, we cannot exit the system since we rescue the exception caused by system's exit signal. Then the system exception will be swallowed without firing bugs to the developers.
begin
rescue => e # equivalent to rescue StandardError => e
end
  1. A custom exception should be inherit from StandardError. There are 2 ways of declaring
CustomException = Class.new(StandardError)
class CustomException < StandardError
end

Tests

Double

There are 5 kinds of mocking in tests.

Dummy object: just a dummy object, used for passing as an argument and doing nothing Stub: Double: Fake: Mock:

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