Class: String
- Includes:
- HeadingHelper
- Defined in:
- lib/open_classes/string/say.rb,
lib/open_classes/string/uniq.rb,
lib/open_classes/string/stripe.rb,
lib/open_classes/string/gte_gte.rb,
lib/open_classes/string/spacing.rb,
lib/open_classes/string/surround.rb,
lib/open_classes/string/uniq_size.rb,
lib/open_classes/string/comma_to_a.rb,
lib/open_classes/string/hyphen_to_a.rb,
lib/open_classes/string/escape_quote.rb,
lib/open_classes/string/justify_char.rb,
lib/open_classes/string/justify_table.rb,
lib/open_classes/string/table_to_array.rb,
lib/open_classes/string/to_tab_heading.rb,
lib/open_classes/string/unescape_quote.rb,
lib/open_classes/string/is_meta_variable.rb,
lib/open_classes/string/to_hatena_heading.rb,
lib/open_classes/string/to_space2_heading.rb,
lib/open_classes/string/to_space4_heading.rb,
lib/open_classes/string/ascii1_other2_size.rb,
lib/open_classes/string/ascii_unicode_table.rb,
lib/open_classes/string/escape_double_quote.rb,
lib/open_classes/string/to_markdown_heading.rb,
lib/open_classes/string/cygwinpath_to_winpath.rb,
lib/open_classes/string/unescape_double_quote.rb,
lib/open_classes/string/winpath_to_cygwinpath.rb,
lib/open_classes/string/ascii_unicode_html_table.rb
Overview
String
Constant Summary collapse
- TAB =
Tab
"\t"
- HATENA =
Hatena
'*'
- SPACE2 =
Space2
' '
- SPACE4 =
Space4
' '
- MARKDOWN =
Markdown
'#'
- SEPARATOR =
Separator
' '
Instance Method Summary collapse
-
#>>(method_name, *args) ⇒ Object
self convert to Array.
-
#ascii1_other2_size ⇒ Object
count string size.
-
#ascii_unicode_html_table ⇒ Object
get ascii_unicode_table.
-
#ascii_unicode_table ⇒ Object
get ascii_unicode_table.
-
#comma_to_a ⇒ Object
comma-format string to array.
-
#cygwinpath_to_winpath ⇒ Object
convert windows path to cygwin path.
-
#escape_double_quote ⇒ Object
escape double quote.
-
#escape_quote ⇒ Object
escape double quote.
-
#hyphen_to_a ⇒ Object
hyphen-format string to array.
-
#is_meta_variable? ⇒ Boolean
is meta variable.
-
#justify_char(separator = '|', position = :left) ⇒ Object
Justify string using separator.
-
#justify_table(position = :left) ⇒ Object
Justify pipe using table format.
-
#say(option = :quote) ⇒ Object
say string.
-
#spacing(options = { char: ' ', size: 1 }) ⇒ Object
get spacing string.
-
#stripe(cap = :upper_cap) ⇒ Object
stripe string.
-
#surround(options = { top_bottom: '-', side: '|' }) ⇒ Object
surround string.
-
#table_to_array ⇒ Object
convert table format string to array.
-
#to_hatena_heading ⇒ Object
create heading string with Emmet-like syntax.
-
#to_markdown_heading ⇒ Object
create heading string with Emmet-like syntax.
-
#to_space2_heading ⇒ Object
create heading string with Emmet-like syntax.
-
#to_space4_heading ⇒ Object
create heading string with Emmet-like syntax.
-
#to_tab_heading ⇒ Object
create heading string with Emmet-like syntax.
-
#unescape_double_quote ⇒ Object
unescape double quote.
-
#unescape_quote ⇒ Object
unescape single quote.
-
#uniq ⇒ Object
return uniq string.
-
#uniq_size ⇒ Object
return uniq size.
-
#winpath_to_cygwinpath ⇒ Object
convert windows path to cygwin path.
Instance Method Details
#>>(method_name, *args) ⇒ Object
self convert to Array. execute each elements
Example
"abc".>> :next # => 'bcd'
"abc".>> :+, "a" # => 'adbdcd'
13 14 15 16 17 18 19 20 21 22 23 |
# 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 ⇒ Object
count string size. ascii => count1, not ascii => count2
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 ⇒ Object
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>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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.is_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 ⇒ Object
get ascii_unicode_table
Example
input
'aあb'
result
|char|ASCII|ascii2 |Unicode|
| a | 97 |1100001| -- |
| あ | -- | -- |0x3042 |
| b | 98 |1100010| -- |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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.is_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 ⇒ Object
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"]
17 18 19 20 |
# 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 ⇒ Object
convert windows path to cygwin path
Examples
'/cygdrive/c/hoge/hoge.txt'.cygwinpath_to_winpath # => 'C:\hoge\hoge.txt'
10 11 12 13 14 15 |
# 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 ⇒ Object
escape double quote.
Examples
'hoge"hige'.escape_double_quote # => 'hoge""hige'
10 11 12 |
# File 'lib/open_classes/string/escape_double_quote.rb', line 10 def escape_double_quote gsub('"', '""') end |
#escape_quote ⇒ Object
escape double quote.
Examples
"hoge'hige".escape_quote # => "hoge''hige"
10 11 12 |
# File 'lib/open_classes/string/escape_quote.rb', line 10 def escape_quote gsub("'", "''") end |
#hyphen_to_a ⇒ Object
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']
17 18 19 20 21 |
# 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 |
#is_meta_variable? ⇒ Boolean
is meta variable.
Examples
'foo'. # => true
'bar'. # => true
'baz'. # => true
''. # => false
'aaa'. # => false
16 17 18 |
# File 'lib/open_classes/string/is_meta_variable.rb', line 16 def MetasyntacticVariable..include? to_sym end |
#justify_char(separator = '|', position = :left) ⇒ Object
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'
20 21 22 23 24 25 26 |
# 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) ⇒ Object
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 |
21 22 23 24 25 26 |
# File 'lib/open_classes/string/justify_table.rb', line 21 def justify_table(position = :left) return self if self.empty? max_sizes = get_column_maxes_table return self if max_sizes.nil? justify_lines_table max_sizes, position end |
#say(option = :quote) ⇒ Object
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-
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 }) ⇒ Object
get spacing string
Example
input
hoge = 'hoge'
hoge.spacing # => 'h o g e'
hoge.spacing({char: '_', size: 2}) # => 'h__o__g__e'
14 15 16 17 18 |
# File 'lib/open_classes/string/spacing.rb', line 14 def spacing( = { char: ' ', size: 1 }) [:char] = ' ' unless [:char] [:size] = 1 unless [:size] chars.to_a.join([:char] * [:size]) end |
#stripe(cap = :upper_cap) ⇒ Object
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
30 31 32 33 34 35 36 37 38 |
# 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: '|' }) ⇒ Object
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!
======
45 46 47 48 49 50 51 52 |
# File 'lib/open_classes/string/surround.rb', line 45 def surround( = { top_bottom: '-', side: '|' }) top_bottom = init_top_bottom() side = init_side() inner_width = line_max top_bottom = top_bottoms(top_bottom, inner_width) ret = *each_line.reduce(["#{top_bottom}"]) { |ret, line|ret << "#{side}#{line.chomp.ljust(inner_width)}#{side}" } ret.push("#{top_bottom}").join("\n") end |
#table_to_array ⇒ Object
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"]]
20 21 22 |
# 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 ⇒ Object
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'
26 27 28 29 |
# 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 ⇒ Object
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'
28 29 30 31 |
# 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 ⇒ Object
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'
26 27 28 29 |
# 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 ⇒ Object
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'
26 27 28 29 |
# 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 ⇒ Object
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'
26 27 28 29 |
# 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 ⇒ Object
unescape double quote.
Examples
'hoge""hige'.unescape_double_quote # => 'hoge"hige'
10 11 12 |
# File 'lib/open_classes/string/unescape_double_quote.rb', line 10 def unescape_double_quote gsub('""', '"') end |
#unescape_quote ⇒ Object
unescape single quote.
Examples
"hoge''h''ige".unescape_quote # => "hoge'hige"
10 11 12 |
# File 'lib/open_classes/string/unescape_quote.rb', line 10 def unescape_quote gsub("''", "'") end |
#uniq ⇒ Object
return uniq string
Examples
'abcdac'.uniq # => 'abcd'
10 11 12 |
# File 'lib/open_classes/string/uniq.rb', line 10 def uniq split('').uniq.join end |
#uniq_size ⇒ Object
return uniq size
Example
'abcdefa'.uniq_size # => 6
'abcdef'.uniq_size # => 6
''.uniq_size # => 0
13 14 15 |
# File 'lib/open_classes/string/uniq_size.rb', line 13 def uniq_size split('').uniq.size end |
#winpath_to_cygwinpath ⇒ Object
convert windows path to cygwin path
Examples
'C:\hoge\hoge.txt'.winpath_to_cygwinpath # => '/cygdrive/c/hoge/hoge.txt'
10 11 12 13 14 15 |
# 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 |