Module: NeuronCheckSystem::Utils
- Defined in:
- lib/neuroncheck/utils.rb
Class Method Summary collapse
-
.backtrace_locations_to_captions(locations) ⇒ Object
Thread::Backtrace::Locationのリストを文字列形式に変換。フレーム数が多すぎる場合は途中を省略.
-
.ordinalize(v) ⇒ Object
指定した整数値を序数文字列にする.
-
.string_join_using_or_conjunction(strings) ⇒ Object
1つ以上の文字列をorで結んだ英語文字列にする.
-
.truncate(str, truncate_at, omission: '...', separator: nil) ⇒ Object
From ActiveSupport (Thanks for Rails Team!) <github.com/rails/rails/tree/master/activesupport>.
Class Method Details
.backtrace_locations_to_captions(locations) ⇒ Object
Thread::Backtrace::Locationのリストを文字列形式に変換。フレーム数が多すぎる場合は途中を省略
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/neuroncheck/utils.rb', line 59 def backtrace_locations_to_captions(locations) locs = nil if locations.size > 9 then locs = (locations[0..3].map{|x| "from #{x.to_s}"} + [" ... (#{locations.size - 8} frames) ..."] + locations[-4..-1].map{|x| "from #{x.to_s}"}) else locs = locations.map{|x| "from #{x.to_s}"} end if locs.size >= 1 then locs.first.sub!(/\A\s*from /, '') end locs end |
.ordinalize(v) ⇒ Object
指定した整数値を序数文字列にする
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/neuroncheck/utils.rb', line 75 def ordinalize(v) if [11,12,13].include?(v % 100) "#{v}th" else case (v % 10) when 1 "#{v}st" when 2 "#{v}nd" when 3 "#{v}rd" else "#{v}th" end end end |
.string_join_using_or_conjunction(strings) ⇒ Object
1つ以上の文字列をorで結んだ英語文字列にする
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/neuroncheck/utils.rb', line 41 def string_join_using_or_conjunction(strings) ret = "" strings.each_with_index do |str, i| case i when 0 # 最初の要素 when strings.size - 1 # 最後の要素 ret << " or " else ret << ", " end ret << str end ret end |
.truncate(str, truncate_at, omission: '...', separator: nil) ⇒ Object
From ActiveSupport (Thanks for Rails Team!) <github.com/rails/rails/tree/master/activesupport>
Truncates a given text after a given length if text is longer than length:
'Once upon a time in a world far far away'.truncate(27)
# => "Once upon a time in a wo..."
Pass a string or regexp :separator to truncate text at a natural break:
'Once upon a time in a world far far away'.truncate(27, separator: ' ')
# => "Once upon a time in a..."
'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
# => "Once upon a time in a..."
The last characters will be replaced with the :omission string (defaults to “…”) for a total length not exceeding length:
'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
# => "And they f... (continued)"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/neuroncheck/utils.rb', line 25 def truncate(str, truncate_at, omission: '...', separator: nil) return str.dup unless str.length > truncate_at omission = omission || '...' length_with_room_for_omission = truncate_at - omission.length stop = \ if separator rindex(separator, length_with_room_for_omission) || length_with_room_for_omission else length_with_room_for_omission end "#{str[0, stop]}#{omission}" end |