Class: String

Inherits:
Object show all
Defined in:
lib/pubsub/string.rb

Instance Method Summary collapse

Instance Method Details

#sub_before(separator) ⇒ Object

Return substring from start to last occurrence separator. In the block form, return all substring “test/test1/test2”.sub_before(“/”) {|item| puts item} test/test1/test2 test/test1 test



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pubsub/string.rb', line 7

def sub_before(separator)
  result = self
  index = size
  begin
    result = result[0, index]
    next if result.empty?
    break unless block_given?
    yield result
  end while index = result.rindex('/')
  result
end