This constructor takes advantage of a technique for Ruby object construction introduced by Andy Hunt and Dave Thomas (see reference), where optional values are set using commands in a block.
Text::Format.new { columns = 72 left_margin = 0 right_margin = 0 first_indent = 4 body_indent = 0 format_style = Text::Format::LEFT_ALIGN extra_space = false abbreviations = {} tag_paragraph = false tag_text = [] nobreak = false nobreak_regex = {} tabstop = 8 text = nil }
As shown above, arg is optional. If arg is specified and is a String, then arg is used as the default value of #text. Alternately, an existing Text::Format object can be used or a Hash can be used. With all forms, a block can be specified.
| Reference: | "Object Construction and Blocks" <www.pragmaticprogrammer.com/ruby/articles/insteval...> |
Source Code
# File action_mailer/vendor/text-format-0.6.3/text/format.rb, line 963 def initialize(arg = nil, &block) case arg when Text::Format __create(arg.text) do @columns = arg.columns @tabstop = arg.tabstop @first_indent = arg.first_indent @body_indent = arg.body_indent @format_style = arg.format_style @left_margin = arg.left_margin @right_margin = arg.right_margin @extra_space = arg.extra_space @tag_paragraph = arg.tag_paragraph @tag_text = arg.tag_text @abbreviations = arg.abbreviations @nobreak = arg.nobreak @nobreak_regex = arg.nobreak_regex @text = arg.text @hard_margins = arg.hard_margins @split_words = arg.split_words @split_rules = arg.split_rules @hyphenator = arg.hyphenator end instance_eval(&block) unless block.nil? when Hash __create do @columns = arg[:columns] || arg['columns'] || @columns @tabstop = arg[:tabstop] || arg['tabstop'] || @tabstop @first_indent = arg[:first_indent] || arg['first_indent'] || @first_indent @body_indent = arg[:body_indent] || arg['body_indent'] || @body_indent @format_style = arg[:format_style] || arg['format_style'] || @format_style @left_margin = arg[:left_margin] || arg['left_margin'] || @left_margin @right_margin = arg[:right_margin] || arg['right_margin'] || @right_margin @extra_space = arg[:extra_space] || arg['extra_space'] || @extra_space @text = arg[:text] || arg['text'] || @text @tag_paragraph = arg[:tag_paragraph] || arg['tag_paragraph'] || @tag_paragraph @tag_text = arg[:tag_text] || arg['tag_text'] || @tag_text @abbreviations = arg[:abbreviations] || arg['abbreviations'] || @abbreviations @nobreak = arg[:nobreak] || arg['nobreak'] || @nobreak @nobreak_regex = arg[:nobreak_regex] || arg['nobreak_regex'] || @nobreak_regex @hard_margins = arg[:hard_margins] || arg['hard_margins'] || @hard_margins @split_rules = arg[:split_rules] || arg['split_rules'] || @split_rules @hyphenator = arg[:hyphenator] || arg['hyphenator'] || @hyphenator end instance_eval(&block) unless block.nil? when String __create(arg, &block) when NilClass __create(&block) else raise TypeError end end
<code/>and<pre/>for code samples.