Class: String

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

Instance Method Summary collapse

Instance Method Details

#dashifyObject

Returns the string with ‘_’ translated to ‘-’

'data_set'.dashify  # => "data-set"


34
35
36
# File 'lib/swivel.rb', line 34

def dashify
  self.tr('_', '-')
end

#numeric?Boolean

Returns true if the string looks numeric

'1283.22'.numeric? # => true
'howdy'.numeric? # => false

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/swivel.rb', line 25

def numeric?
  # work around the bug in ruby ^ regexp matching
  string_sans_newlines = self.gsub(/\r|\n/, ' ')
  (string_sans_newlines =~ /^-?\d+(\.\d+|\d*)$/) != nil
end

#to_xml_tagObject

Returns a string as a suitable xml tag by underscoring and dashifying. Not perfect, but serves its purpose.

'DataSet'.to_xml_tag # => "data-set"


49
50
51
# File 'lib/swivel.rb', line 49

def to_xml_tag
  self.underscore.dasherize
end

#undashifyObject

Returns the string with ‘-’ translated to ‘_’

'data-set'.undashify  # => "data_set"


41
42
43
# File 'lib/swivel.rb', line 41

def undashify
  self.tr('-', '_')
end