Module: Ykutils::StringUtils
- Included in:
- FileOpUtils
- Defined in:
- lib/ykutils/stringutils.rb
Instance Method Summary collapse
- #get_basename_ext(itx) ⇒ Object
- #get_normalized_string(str) ⇒ Object
- #indent(num) ⇒ Object
- #make_content_from_template(template_ary, tag_value_hash, separator) ⇒ Object
- #nil_to_nullstring(str) ⇒ Object
- #normal_string?(str) ⇒ Boolean
- #print_hier(data, level) ⇒ Object
- #print_hier2(data, level) ⇒ Object
Instance Method Details
#get_basename_ext(itx) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ykutils/stringutils.rb', line 7 def get_basename_ext(itx) bn0 = itx.basename.to_s if RUBY_VERSION > "1.9" # bn0.encode!( "internal" ) bn0.encode! "UTF-8" end list = bn0.split(".") case list.size when 0 bname = "" ext = "" when 1 bname = list[0] ext = "" when 2 bname = list[0] ext = list[1] else bname_size = list.size - 1 bname = list[0, bname_size].join(".") ext = list[-1] end [bname, ext] end |
#get_normalized_string(str) ⇒ Object
41 42 43 44 45 |
# File 'lib/ykutils/stringutils.rb', line 41 def get_normalized_string(str) ret = "" ret = str.strip if str ret end |
#indent(num) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/ykutils/stringutils.rb', line 64 def indent(num) n ||= 0 @indent_hash ||= {} @indent_hash[num] ||= " " * n @indent_hash[num] end |
#make_content_from_template(template_ary, tag_value_hash, separator) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ykutils/stringutils.rb', line 47 def make_content_from_template(template_ary, tag_value_hash, separator) content_ary = [] reg_hash = {} tag_value_hash.each_key do |k| reg_hash[k] = Regexp.new(separator + k + separator) end template_ary.each do |line| reg_hash.each do |k, v| line = line.sub(v, tag_value_hash[k]) end content_ary << line end content_ary end |
#nil_to_nullstring(str) ⇒ Object
3 4 5 |
# File 'lib/ykutils/stringutils.rb', line 3 def nil_to_nullstring(str) str || "" end |
#normal_string?(str) ⇒ Boolean
32 33 34 35 36 37 38 39 |
# File 'lib/ykutils/stringutils.rb', line 32 def normal_string?(str) ret = false if str s = str.strip ret = true if s != "" && (s =~ %r{^[0-9A-Za-z_:./\-]+$}) end ret end |
#print_hier(data, level) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ykutils/stringutils.rb', line 71 def print_hier(data, level) case data.class.to_s when "Hash" data.each_value do |v| # puts "#{indent(level)}#{k}|#{v.class} #{if v.class.to_s == 'String' then v.size else '' end }" # puts "#{indent(level)}#{k}|#{v.class}" print_hier(v, level + 1) end when "Array" data.each do |v| print_hier(v, level + 1) end # else # puts "#{indent(level)}|#{data.class}" end end |
#print_hier2(data, level) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ykutils/stringutils.rb', line 88 def print_hier2(data, level) case data.class.to_s when "Hash" data.each do |k, v| # puts "#{indent(level)}#{k}|#{v.class} #{if v.class.to_s == 'String' then v.size else '' end }" str = "#{indent(level)}#{k}|#{v.class}" if v.instance_of?(Array) num = v.size str += "|#{num}" "#{str}###################" if num > 1 end # puts str print_hier_2(v, level + 1) if k != "group" end when "Array" data.each do |v| print_hier_2(v, level + 1) end # else # puts "#{indent(level)}|#{data.class}" end end |