Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/mb_string/core_ext/string.rb
Instance Method Summary collapse
- #mb_center(width, pad_str = " ") ⇒ Object
- #mb_ljust(width, pad_str = " ") ⇒ Object
- #mb_rjust(width, pad_str = " ") ⇒ Object
- #mb_truncate(truncate_at, options = {}) ⇒ Object
Instance Method Details
#mb_center(width, pad_str = " ") ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mb_string/core_ext/string.rb', line 18 def mb_center(width, pad_str = " ") return center(width, pad_str) if ascii_only? && pad_str.ascii_only? mb_execute(width) do |pad_size| left_pad_size = pad_size / 2 right_pad_size = pad_size - left_pad_size right_padding = mb_build_padding(right_pad_size, pad_str) left_padding = mb_build_padding(left_pad_size, pad_str, is_append_right: false) left_padding + self + right_padding end end |
#mb_ljust(width, pad_str = " ") ⇒ Object
4 5 6 7 8 9 |
# File 'lib/mb_string/core_ext/string.rb', line 4 def mb_ljust(width, pad_str = " ") return ljust(width, pad_str) if ascii_only? && pad_str.ascii_only? mb_execute(width) do |pad_size| self + mb_build_padding(pad_size, pad_str) end end |
#mb_rjust(width, pad_str = " ") ⇒ Object
11 12 13 14 15 16 |
# File 'lib/mb_string/core_ext/string.rb', line 11 def mb_rjust(width, pad_str = " ") return rjust(width, pad_str) if ascii_only? && pad_str.ascii_only? mb_execute(width) do |pad_size| mb_build_padding(pad_size, pad_str, is_append_right: false) + self end end |
#mb_truncate(truncate_at, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mb_string/core_ext/string.rb', line 31 def mb_truncate(truncate_at, = {}) return dup unless display_width > truncate_at omission = [:omission] || "..." length_with_room_for_omission = truncate_at - omission.display_width size = 0 slice = "" each_char do |c| char_size = c.display_width if size + char_size > length_with_room_for_omission slice << omission break end size += char_size slice << c end slice end |