Module: SSCBot::Util

Defined in:
lib/ssc.bot/util.rb

Overview

Your typical utility methods that should be moved into a separate Gem one day…

Author:

  • Jonathan Bradley Whited

Since:

  • 0.1.0

Constant Summary collapse

OS =

Since:

  • 0.1.0

os
RUBY_ENGINE =

Since:

  • 0.1.0

ruby_engine

Class Method Summary collapse

Class Method Details

.os(host_os = RbConfig::CONFIG['host_os']) ⇒ Object

Since:

  • 0.1.0



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ssc.bot/util.rb', line 23

def self.os(host_os=RbConfig::CONFIG['host_os'])
  os = :unknown

  case host_os
  when /darwin/i
    os = :macos
  # I think 'cygwin' here makes sense.
  when /linux|arch|cygwin/i
    os = :linux
  else
    # Here so that 'win' doesn't capture 'darwin'.
    case host_os
    # windows|mswin|bccwin|wince
    when /win|mingw|emx/i
      os = :windows
    end
  end

  return os
end

.quote_str_or_regex(value) ⇒ Object

Since:

  • 0.1.0



45
46
47
48
49
50
51
# File 'lib/ssc.bot/util.rb', line 45

def self.quote_str_or_regex(value)
  if value.respond_to?(:source)
    return value.source.gsub(' ','\\ ') # For //x
  else
    return Regexp.quote(value)
  end
end

.ruby_engineObject

Since:

  • 0.1.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ssc.bot/util.rb', line 53

def self.ruby_engine
  engines = [
    defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : nil,
    RbConfig::CONFIG['ruby_install_name'],
    RbConfig::CONFIG['rubyw_install_name'],
    RbConfig::CONFIG['RUBY_INSTALL_NAME'],
    RbConfig::CONFIG['RUBYW_INSTALL_NAME'],
    RbConfig.ruby,
  ].join('|').downcase

  if engines.include?('jruby')
    return :jruby
  elsif engines.include?('truffleruby')
    return :truffleruby
  end

  return :ruby
end

.u_blank?(str) ⇒ Boolean

Universally, is str empty after stripping or nil?

Returns:

  • (Boolean)

Since:

  • 0.1.0



74
75
76
# File 'lib/ssc.bot/util.rb', line 74

def self.u_blank?(str)
  return str.nil? || str.empty? || u_strip(str).empty?
end

.u_lstrip(str) ⇒ Object

Universally, left strip str‘s leading (head) space.

Since:

  • 0.1.0



79
80
81
82
# File 'lib/ssc.bot/util.rb', line 79

def self.u_lstrip(str)
  return nil if str.nil?
  return str.gsub(/\A[[:space:]]+/,'')
end

.u_rstrip(str) ⇒ Object

Universally, right strip str‘s trailing (tail) space.

Since:

  • 0.1.0



85
86
87
88
# File 'lib/ssc.bot/util.rb', line 85

def self.u_rstrip(str)
  return nil if str.nil?
  return str.gsub(/[[:space:]]+\z/,'')
end

.u_strip(str) ⇒ Object

Universally, strip str‘s space.

Since:

  • 0.1.0



91
92
93
94
# File 'lib/ssc.bot/util.rb', line 91

def self.u_strip(str)
  return nil if str.nil?
  return str.gsub(/\A[[:space:]]+|[[:space:]]+\z/,'')
end