Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Created November 13, 2013 21:35
Show Gist options
  • Select an option

  • Save invisiblefunnel/7456808 to your computer and use it in GitHub Desktop.

Select an option

Save invisiblefunnel/7456808 to your computer and use it in GitHub Desktop.
# https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activesupport/lib/active_support/core_ext/object/try.rb
class Object
def try(*a, &b)
if a.empty? && block_given?
yield self
else
public_send(*a, &b) if respond_to?(a.first)
end
end
end
class NilClass
def try(*args)
nil
end
end
# https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activesupport/lib/active_support/core_ext/string/strip.rb
class String
def strip_heredoc
indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
gsub(/^[ \t]{#{indent}}/, '')
end
end
puts <<-CHARS
A
B
C
D
CHARS
# A
# B
# C
# D
puts <<-CHARS.strip_heredoc
E
F
G
H
CHARS
#E
# F
# G
# H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment