Class: String

Inherits:
Object show all
Defined in:
lib/sc-core-ext/string.rb

Instance Method Summary collapse

Instance Method Details

#dehumanizeObject

The inverse of ActiveSupport::Inflection#humanize: Lowercases the first letter, and turns spaces into underscores. This is meant to assist in creating method names. A camelCase method name can be created using #dehumanize:

"say_hello_to_the_world".camelize.dehumanize  # => "sayHelloToTheWorld"

This can also be used for creating permalinks:

"Say hello to the world".dehumanize           # => "say_hello_to_the_world"


8
9
10
# File 'lib/sc-core-ext/string.rb', line 8

def dehumanize
  self.camelize.gsub(/^([A-Z])/) { |x| x.downcase }.gsub(/ /, '_')
end

#depunctuateObject

This method assumes that this string represents a Ruby-like method name. Removes question marks and exclamation marks from this string, prepending “is_” or “force_”, respectively. If neither of these punctuation marks exist, the original string is returned.



19
20
21
22
23
24
25
26
# File 'lib/sc-core-ext/string.rb', line 19

def depunctuate
  if self[/\?/]
    "is_"+self.gsub(/\?/, '')
  elsif self[/\!/]
    "force_"+self.gsub(/\!/, '')
  else self
  end
end

#hex_to_binObject



38
39
40
41
42
43
# File 'lib/sc-core-ext/string.rb', line 38

def hex_to_bin
  temp = gsub("\s", "");
  ret = []
  (0...temp.length / 2).each { |index| ret[index] = [temp[index*2, 2]].pack("H2") }
  return ret.join
end

#indent(unused = 0) ⇒ Object

Returns a copy of itself, except that the first character is preceded by a tabstop (t) and a tabstop also follows every subsequent newline (n) character.

The unused argument is for compatibility with treetop, a cucumber dependency.



33
34
35
# File 'lib/sc-core-ext/string.rb', line 33

def indent(unused = 0)
  "\t#{self.gsub(/\n/m, "\n\t")}"
end

#parenthesize(with = "()") ⇒ Object



12
13
14
# File 'lib/sc-core-ext/string.rb', line 12

def parenthesize(with = "()")
  with[0].chr + self + with[(with.length == 1 ? 0 : 1)].chr
end