class String

String

String

String

String

String

String

String

String

String

String

String

String

String

String

Constants

HATENA

Hatena

MARKDOWN

Markdown

SEPARATOR

Separator

SPACE2

Space2

SPACE4

Space4

TAB

Tab

Public Instance Methods

>>(method_name, *args) click to toggle source

self convert to Array. execute each elements

Example

"abc".>> :next # => 'bcd'
"abc".>> :+, "a" # => 'adbdcd'
# File lib/open_classes/string/gte_gte.rb, line 13
def >>(method_name, *args)
  return self unless [Symbol, String, Proc].include? method_name.class
  array_context = split('').>>
  rets =
    if args.size.nil? || args.size == 0
      array_context.send method_name
    else
      array_context.send method_name, *args
    end
  rets.join
end
ascii1_other2_size() click to toggle source

count string size. ascii => count1, not ascii => count2

# File lib/open_classes/string/ascii1_other2_size.rb, line 8
def ascii1_other2_size
  chars.to_a.reduce(0) do |sum, v|
    case v.ord
    when 65_393..65_437
      sum += 1
    when 1..127
      sum += 1
    else
      sum += 2
    end
    sum
  end
end
ascii_unicode_html_table() click to toggle source

get ascii_unicode_table

Example

input

'aあb'

result

<table>
  <tr>
    <th>char</th>
    <th>ASCII</th>
    <th>ascii2</th>
    <th>Unicode</th>
  </tr>
  <tr>
    <td>a</td>
    <td>97</td>
    <td>1100001</td>
    <td>--</td>
  </tr>
  <tr>
    <td>あ</td>
    <td>--</td>
    <td>--</td>
    <td>0x3042</td>
  </tr>
  <tr>
    <td>b</td>
    <td>98</td>
    <td>1100010</td>
    <td>--</td>
  </tr>
</table>
# File lib/open_classes/string/ascii_unicode_html_table.rb, line 43
def ascii_unicode_html_table
  ret = ["<table>\n  <tr>\n    <th>char</th>\n    <th>ASCII</th>\n    <th>ascii2</th>\n    <th>Unicode</th>\n  </tr>"]
  chars.each do |c|
    each_ret = []
    each_ret << "  <tr>\n    <td>#{c}</td>"
    if c.ord.ascii?
      each_ret << "    <td>#{c.ord}</td>"
      each_ret << "    <td>#{c.ord.to_s(2)}</td>"
      each_ret << "    <td>--</td>\n  </tr>"
    else
      each_ret << '    <td>--</td>'
      each_ret << '    <td>--</td>'
      each_ret << "    <td>0x#{c.ord.to_s(16)}</td>\n  </tr>"
    end
    ret << each_ret.join("\n")
  end
  (ret.join("\n") + "\n</table>\n")
end
ascii_unicode_table() click to toggle source

get ascii_unicode_table

Example

input

'aあb'

result

|char|ASCII|ascii2 |Unicode|
| a  | 97  |1100001|  --   |
| あ | --  |  --   |0x3042 |
| b  | 98  |1100010|  --   |
# File lib/open_classes/string/ascii_unicode_table.rb, line 22
def ascii_unicode_table
  ret = ['|char|ASCII|ascii2|Unicode|']
  chars.each do |c|
    each_ret = []
    each_ret << "|#{c}"
    if c.ord.ascii?
      each_ret << c.ord
      each_ret << c.ord.to_s(2)
      each_ret << '--'
    else
      each_ret << '--'
      each_ret << '--'
      each_ret << "0x#{c.ord.to_s(16)}"
    end
    ret << each_ret.join('|') + '|'
  end
  (ret.join("\n") + "\n").justify_table(:center)
end
comma_to_a() click to toggle source

comma-format string to array.

Examples

space commma case

'1, 5, 9'.comma_to_a # => ["1", "5", "9"]

commma case

'1,5,9'.comma_to_a # => ["1", "5", "9"]
# File lib/open_classes/string/comma_to_a.rb, line 17
def comma_to_a
  return self unless self.include? ','
  gsub(' ', '').split(',')
end
cygwinpath_to_winpath() click to toggle source

convert windows path to cygwin path

Examples

'/cygdrive/c/hoge/hoge.txt'.cygwinpath_to_winpath # => 'C:\hoge\hoge.txt'
# File lib/open_classes/string/cygwinpath_to_winpath.rb, line 10
def cygwinpath_to_winpath
  return self unless match(%r{/cygdrive/})
  drive = scan(%r{/cygdrive/(\w)/}).first.first.upcase
  dir_file = scan(%r{/cygdrive/\w/(.*)}).first.first.gsub('/', '\\')
  "#{drive}:\\#{dir_file}"
end
escape_double_quote() click to toggle source

escape double quote.

Examples

'hoge"hige'.escape_double_quote # => 'hoge""hige'
# File lib/open_classes/string/escape_double_quote.rb, line 10
def escape_double_quote
  gsub('"', '""')
end
escape_quote() click to toggle source

escape double quote.

Examples

"hoge'hige".escape_quote # => "hoge''hige"
# File lib/open_classes/string/escape_quote.rb, line 10
def escape_quote
  gsub("'", "''")
end
hyphen_to_a() click to toggle source

hyphen-format string to array.

Examples

number case

'1-5'.hyphen_to_a # => [1, 2, 3, 4, 5]

alphabet case

'"a"-"e"'.hyphen_to_a # => ['a', 'b', 'c', 'd', 'e']
# File lib/open_classes/string/hyphen_to_a.rb, line 17
def hyphen_to_a
  return self unless self.include? '-'
  return self unless count('-') == 1
  eval "[*#{gsub('-', '..')}]", binding
end
justify_char(separator = '|', position = :left) click to toggle source

Justify string using separator

before justify

print 'hoge' # => 'hoge'
print 'hoge' * 2 # => 'hogehoge'
print 'hoge' + 'hige' # => 'hogehige'

after justify

print 'hoge'          # => 'hoge'
print 'hoge' * 2      # => 'hogehoge'
print 'hoge' + 'hige' # => 'hogehige'
# File lib/open_classes/string/justify_char.rb, line 20
def justify_char(separator = '|', position = :left)
  return self if empty?
  return self unless include? separator
  max_sizes = get_column_maxes(separator)
  return self if max_sizes.nil?
  justify_lines max_sizes, position, separator
end
justify_table(position = :left) click to toggle source

Justify pipe using table format

before justify

|* first name|* family name|
|eiichiro|oda|
|akira|toriyama|
|yusei|matsui|

after justify

|* first name|* family name|
|eiichiro    |oda          |
|akira       |toriyama     |
|yusei       |matsui       |
# File lib/open_classes/string/justify_table.rb, line 21
def justify_table(position = :left)
  return self if self.empty?
  max_sizes = column_maxes_table
  return self if max_sizes.nil?
  justify_lines_table max_sizes, position
end
meta_variable?() click to toggle source

is meta variable.

Examples

'foo'.meta_variable? # => true
'bar'.meta_variable? # => true
'baz'.meta_variable? # => true
''.meta_variable? # => false
'aaa'.meta_variable? # => false
# File lib/open_classes/string/is_meta_variable.rb, line 16
def meta_variable?
  MetasyntacticVariable.meta_variables.include? to_sym
end
say(option = :quote) click to toggle source

say string.

Options

  • option - quote, dquote, bracket, hyphen

Examples

default case

'hoge'.say # => 'hoge'

quote case

'hoge'.say(:quote) # => 'hoge'

dquote case

'hoge'.say(:dquote) # => "hoge"

bracket case

'hoge'.say(:bracket) # => [hoge]

hyphen case

'hoge'.say(:hyphen) # => -hoge-
# File lib/open_classes/string/say.rb, line 33
def say(option = :quote)
  case option
  when :quote
    "'#{self}'"
  when :dquote
    "\"#{self}\""
  when :bracket
    "[#{self}]"
  when :hyphen
    "-#{self}-"
  else
    fail ArgumentError, "invalid value #{option}"
  end
end
spacing(options = { char: ' ', size: 1 }) click to toggle source

get spacing string

Example

input

hoge = 'hoge'
hoge.spacing # => 'h o g e'
hoge.spacing({char: '_', size: 2}) # => 'h__o__g__e'
# File lib/open_classes/string/spacing.rb, line 14
def spacing(options = { char: ' ', size: 1 })
  options[:char] = ' ' unless options[:char]
  options[:size] = 1 unless options[:size]
  chars.to_a.join(options[:char] * options[:size])
end
stripe(cap = :upper_cap) click to toggle source

stripe string.

Options

  • :lower_cap - start lower char.

Examples

default case

'hoge'.stripe # => HoGe

lower_cap case

'hoge'.stripe :lower_cap # => hOgE

empty case

''.stripe # => ''

nil case

hoge = nil
hoge.stripe # => nil
# File lib/open_classes/string/stripe.rb, line 30
def stripe(cap = :upper_cap)
  updowns = %w(upcase downcase)
  index = cap == :lower_cap ? 1 : 0
  chars.reduce([]) do |ret, char|
    ret << char.send(updowns[index % 2])
    index += 1
    ret
  end.join
end
surround(options = { top_bottom: '-', side: '|' }) click to toggle source

surround string.

Options

  • :top_bottom - set top and bottom charactor

  • :side - set right and left charactor

Examples

single line, no option case

'hoge'.surround

result

------ 
|hoge|
------

multi line, no option case

"hoge\na".surround

result

------ 
|hoge|
|a   |
------

single line, both option case

'hoge'.surround top_bottom: '=', side: '!'

result

======
!hoge!
======
# File lib/open_classes/string/surround.rb, line 45
def surround(options = { top_bottom: '-', side: '|' })
  top_bottom = init_top_bottom(options)
  side = init_side(options)
  inner_width = line_max
  top_bottom = top_bottoms(top_bottom, inner_width)
  ret = *each_line.reduce(["#{top_bottom}"]) { |a, e|a << "#{side}#{e.chomp.ljust(inner_width)}#{side}" }
  ret.push("#{top_bottom}").join("\n")
end
table_to_array() click to toggle source

convert table format string to array.

Example

sample case.

BEFORE =<<-EOS
|header1|header2 |header3|
|line1_1| line1_2|line1_3|
EOS
BEFORE.table_to_array

result

[["header1", "header2", "header3"], ["line1_1", "line1_2", "line1_3"]]
# File lib/open_classes/string/table_to_array.rb, line 20
def table_to_array
  split("\n").map { |v|v.split('|')[1..-1].map(&:strip) }
end
to_hatena_heading() click to toggle source

create heading string with Emmet-like syntax.

Examples

> case

'hoge>hige'.to_hatena_heading # => '*hoge\n**hige'

+ case

'hoge+hige'.to_hatena_heading # => '*hoge\n*hige'

^ case

'hoge>hige^hege'.to_hatena_heading # => '*hoge\n**hige\n*hege'
# File lib/open_classes/string/to_hatena_heading.rb, line 26
def to_hatena_heading
  heading = to_heading
  to_hatena heading
end
to_markdown_heading() click to toggle source

create heading string with Emmet-like syntax.

Examples

> case

'hoge>hige'.to_markdown_heading # => '# hoge\n## hige'

+ case

'hoge+hige'.to_markdown_heading # => '# hoge\n# hige'

^ case

'hoge>hige^hege'.to_markdown_heading # => '# hoge\n## hige\n# hege'
# File lib/open_classes/string/to_markdown_heading.rb, line 28
def to_markdown_heading
  heading = to_heading
  to_markdown heading
end
to_space2_heading() click to toggle source

create heading string with Emmet-like syntax.

Examples

> case

'hoge>hige'.to_space2_heading # => 'hoge\n  hige'

+ case

'hoge+hige'.to_space2_heading # => 'hoge\nhige'

^ case

'hoge>hige^hege'.to_space2_heading # => 'hoge\n  hige\nhege'
# File lib/open_classes/string/to_space2_heading.rb, line 26
def to_space2_heading
  heading = to_heading
  to_space2 heading
end
to_space4_heading() click to toggle source

create heading string with Emmet-like syntax.

Examples

> case

'hoge>hige'.to_space4_heading # => 'hoge\n    hige'

+ case

'hoge+hige'.to_space4_heading # => 'hoge\nhige'

^ case

'hoge>hige^hege'.to_space4_heading # => 'hoge\n    hige\nhege'
# File lib/open_classes/string/to_space4_heading.rb, line 26
def to_space4_heading
  heading = to_heading
  to_space4 heading
end
to_tab_heading() click to toggle source

create heading string with Emmet-like syntax.

Examples

> case

'hoge>hige'.to_tab_heading # => 'hoge\n\thige'

+ case

'hoge+hige'.to_tab_heading # => 'hoge\nhige'

^ case

'hoge>hige^hege'.to_tab_heading # => 'hoge\n\thige\nhege'
# File lib/open_classes/string/to_tab_heading.rb, line 26
def to_tab_heading
  heading = to_heading
  to_tab heading
end
unescape_double_quote() click to toggle source

unescape double quote.

Examples

'hoge""hige'.unescape_double_quote # => 'hoge"hige'
# File lib/open_classes/string/unescape_double_quote.rb, line 10
def unescape_double_quote
  gsub('""', '"')
end
unescape_quote() click to toggle source

unescape single quote.

Examples

"hoge''h''ige".unescape_quote # => "hoge'hige"
# File lib/open_classes/string/unescape_quote.rb, line 10
def unescape_quote
  gsub("''", "'")
end
uniq() click to toggle source

return uniq string

Examples

'abcdac'.uniq # => 'abcd'
# File lib/open_classes/string/uniq.rb, line 10
def uniq
  split('').uniq.join
end
uniq_size() click to toggle source

return uniq size

Example

'abcdefa'.uniq_size # => 6
'abcdef'.uniq_size # => 6
''.uniq_size # => 0
# File lib/open_classes/string/uniq_size.rb, line 13
def uniq_size
  split('').uniq.size
end
winpath_to_cygwinpath() click to toggle source

convert windows path to cygwin path

Examples

'C:\hoge\hoge.txt'.winpath_to_cygwinpath # => '/cygdrive/c/hoge/hoge.txt'
# File lib/open_classes/string/winpath_to_cygwinpath.rb, line 10
def winpath_to_cygwinpath
  return self unless match(/\w:\\/)
  drive = scan(/(\w):\\/).first.first.downcase
  dir_file = scan(/\w:\\(.*)/).first.first.gsub('\\', '/')
  "/cygdrive/#{drive}/#{dir_file}"
end

Private Instance Methods

column_maxes_table() click to toggle source
# File lib/open_classes/string/justify_table.rb, line 30
def column_maxes_table
  max_sizes = []
  each_line do |line|
    return nil unless table? line
    columns = get_columuns_table(line)
    max_sizes = get_column_max_table(columns, max_sizes)
  end
  max_sizes
end
get_column_max(columns, max_sizes) click to toggle source
# File lib/open_classes/string/justify_char.rb, line 69
def get_column_max(columns, max_sizes)
  columns.each_with_index do |column, index|
    current_size = column.ascii1_other2_size
    if max_sizes[index].nil?
      max_sizes << current_size
      next
    end
    max_sizes[index] = current_size if current_size > max_sizes[index]
  end
  max_sizes
end
get_column_max_table(columns, max_sizes) click to toggle source
# File lib/open_classes/string/justify_table.rb, line 70
def get_column_max_table(columns, max_sizes)
  columns.each_with_index do |column, index|
    current_size = column.ascii1_other2_size
    # current_size = column.size
    if max_sizes[index].nil?
      max_sizes << current_size
      next
    end
    max_sizes[index] = current_size if current_size > max_sizes[index]
  end
  max_sizes
end
get_column_maxes(separator) click to toggle source
# File lib/open_classes/string/justify_char.rb, line 30
def get_column_maxes(separator)
  max_sizes = []
  each_line do |line|
    columns = get_columuns(line, separator)
    max_sizes = get_column_max(columns, max_sizes)
  end
  max_sizes
end
get_columuns(line, separator) click to toggle source
# File lib/open_classes/string/justify_char.rb, line 65
def get_columuns(line, separator)
  line.split(separator)
end
get_columuns_table(line) click to toggle source
# File lib/open_classes/string/justify_table.rb, line 66
def get_columuns_table(line)
  line.split('|')[1..-2]
end
init_side(options) click to toggle source
# File lib/open_classes/string/surround.rb, line 60
def init_side(options)
  options[:side].nil? ? '|' : options[:side]
end
init_top_bottom(options) click to toggle source
# File lib/open_classes/string/surround.rb, line 56
def init_top_bottom(options)
  options[:top_bottom].nil? ? '-' : options[:top_bottom]
end
justified_column(column, max_size, diff, position) click to toggle source
# File lib/open_classes/string/justify_char.rb, line 53
def justified_column(column, max_size, diff, position)
  pos = max_size - diff
  case position
  when :left
    column.ljust(pos)
  when :right
    column.rjust(pos)
  when :center
    column.center(pos)
  end
end
justified_column_table(column, max_size, diff, position) click to toggle source
# File lib/open_classes/string/justify_table.rb, line 54
def justified_column_table(column, max_size, diff, position)
  pos = max_size - diff
  case position
  when :left
    column.ljust(pos)
  when :right
    column.rjust(pos)
  when :center
    column.center(pos)
  end
end
justify_lines(max_sizes, position, separator) click to toggle source
# File lib/open_classes/string/justify_char.rb, line 39
def justify_lines(max_sizes, position, separator)
  ret = []
  each_line do |line|
    columns = get_columuns(line, separator)
    line_ret = []
    columns.each_with_index do |column, cnt|
      diff = column.ascii1_other2_size - column.size
      line_ret << justified_column(column, max_sizes[cnt], diff, position)
    end
    ret << line_ret.join(separator).gsub(/ +$/m, '')
  end
  ret.join
end
justify_lines_table(max_sizes, position) click to toggle source
# File lib/open_classes/string/justify_table.rb, line 40
def justify_lines_table(max_sizes, position)
  ret = []
  each_line do |line|
    columns = get_columuns_table(line)
    line_ret = []
    columns.each_with_index do |column, cnt|
      diff = column.ascii1_other2_size - column.size
      line_ret << justified_column_table(column, max_sizes[cnt], diff, position)
    end
    ret << "|#{line_ret.join('|')}|"
  end
  ret.join("\n") + "\n"
end
line_max() click to toggle source
# File lib/open_classes/string/surround.rb, line 68
def line_max
  return 0 if self.empty?
  each_line.max_by(&:size).chomp.size
end
table?(text) click to toggle source
# File lib/open_classes/string/justify_table.rb, line 83
def table?(text)
  text.count('|') > 0
end
to_hatena(heading) click to toggle source
# File lib/open_classes/string/to_hatena_heading.rb, line 32
def to_hatena(heading)
  to_head(heading, HATENA, start_level: 1)
end
to_markdown(heading) click to toggle source
# File lib/open_classes/string/to_markdown_heading.rb, line 34
def to_markdown(heading)
  to_head(heading, MARKDOWN, separator: SEPARATOR, start_level: 1)
end
to_space2(heading) click to toggle source
# File lib/open_classes/string/to_space2_heading.rb, line 32
def to_space2(heading)
  to_head(heading, SPACE2)
end
to_space4(heading) click to toggle source
# File lib/open_classes/string/to_space4_heading.rb, line 32
def to_space4(heading)
  to_head(heading, SPACE4)
end
to_tab(heading) click to toggle source
# File lib/open_classes/string/to_tab_heading.rb, line 32
def to_tab(heading)
  to_head(heading, TAB)
end
top_bottoms(top_bottom, inner_width) click to toggle source
# File lib/open_classes/string/surround.rb, line 64
def top_bottoms(top_bottom, inner_width)
  top_bottom * (inner_width + 2)
end