Skip to content

Instantly share code, notes, and snippets.

@paulgoetze
Last active May 13, 2020 18:28
Show Gist options
  • Save paulgoetze/ab7a850007e314fd7fad to your computer and use it in GitHub Desktop.
Save paulgoetze/ab7a850007e314fd7fad to your computer and use it in GitHub Desktop.
Plain text in Ruby
class PlainText
class << self
def write(&block)
return unless block_given?
@previous_line = current_line
@line = []
@paragraph = []
undefine_methods
instance_eval(&block)
(@paragraph << @line.join(' ')).join("\n").strip
end
def method_missing(name, *args, &block)
if current_line == @previous_line
@line.unshift(name)
elsif current_line > @previous_line
@paragraph << @line.join(' ')
@line = [name.to_s]
@previous_line = current_line
else
super
end
end
private
def current_line
caller[1].split(':')[1].to_i
end
def undefine_methods
untouchable_methods = [
:object_id,
:__send__,
:instance_eval,
:method_missing
]
public_methods.each do |method|
unless untouchable_methods.include?(method)
instance_eval("undef :#{method}")
end
end
end
end
end
# Example plain text
text = PlainText.write do
You can only use exclamation marks AND question marks as punctuation!
Keywords that are used In Ruby have to be rewritten with uppercased letters like AND Or OR Or In!
So you can shout a lot! Or you can ask a lot of questions?
Even method names like public_methods Or send can be used In the text!
Because we undefined them!
end
puts text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment