Class: String

Inherits:
Object show all
Defined in:
lib/chef/sugar/core_extensions/string.rb

Overview

require_relative '../constraints'

Direct Known Subclasses

Chef::Sugar::Constraints::Version

Instance Method Summary collapse

Instance Method Details

#flushString

Left-flush a string based off of the number of whitespace characters on the first line. This is especially useful for heredocs when whitespace matters.

Examples:

Remove leading whitespace and flush

<<-EOH.flush
  def method
    'This is a string!'
  end
EOH #=>"def method\n  'This is a string!'\nend"

Returns:


63
64
65
# File 'lib/chef/sugar/core_extensions/string.rb', line 63

def flush
  gsub(/^#{self[/\A\s*/]}/, '').chomp
end

#satisfied_by?(version) ⇒ Boolean

Treat strings as version constraints.

Examples:

Using pure string objects like constraints

'~> 1.2.0'.satisfied_by?('1.2.3')

Parameters:

  • version (String)

    the version to check if it is satisfied

Returns:

  • (Boolean)

See Also:


46
47
48
# File 'lib/chef/sugar/core_extensions/string.rb', line 46

def satisfied_by?(version)
  Chef::Sugar::Constraints::Constraint.new(dup).satisfied_by?(version)
end

#satisfies?(*constraints) ⇒ Boolean

Treat strings as version objects.

Examples:

Using pure string objects like versions

'1.2.3'.satisfies?('~> 1.2.0')

Parameters:

Returns:

  • (Boolean)

See Also:


31
32
33
# File 'lib/chef/sugar/core_extensions/string.rb', line 31

def satisfies?(*constraints)
  Chef::Sugar::Constraints::Version.new(dup).satisfies?(*constraints)
end