Class: CLIntegracon::LazyString

Inherits:
Object
  • Object
show all
Defined in:
lib/CLIntegracon/formatter.rb

Overview

A LazyString is constructed by a block, but only evaluated when needed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ LazyString

Initialize a LazyString

Parameters:

  • (String)] (Block () -)

    block the block which returns a string, called by #to_s


17
18
19
# File 'lib/CLIntegracon/formatter.rb', line 17

def initialize(&block)
  @proc = block
end

Instance Attribute Details

#procProc (readonly)

Returns the closure which will be used to build the string

Returns:

  • (Proc)

    the closure which will be used to build the string


10
11
12
# File 'lib/CLIntegracon/formatter.rb', line 10

def proc
  @proc
end

Instance Method Details

#to_strString Also known as: to_s

Calls the underlying proc to build the string. The result will be memorized, so subsequent calls of this method will not cause that the proc will be called again.

Returns:

  • (String)

27
28
29
# File 'lib/CLIntegracon/formatter.rb', line 27

def to_str
  @string ||= proc.call().to_s
end