Created
March 2, 2011 20:15
-
-
Save sgonyea/851651 to your computer and use it in GitHub Desktop.
rbx issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'strscan' | |
module StringStubber | |
WORD = /\w+[\!\.\?]*[^\s,]/ | |
def scanner | |
@scanner ||= StringScanner.new(self) | |
end | |
# Scans ahead one word position, and returns it | |
# @param [StringScanner] scanner | |
# @return [String] | |
def scan_word(scanner) | |
scanner.scan_until(WORD) | |
end | |
def stub_words(max_words, save_pos=false) | |
scanner.reset unless save_pos | |
if max_words < 0 | |
rev_stub_words(max_words.abs, save_pos) | |
else | |
scan_words(scanner, max_words) | |
end | |
end | |
# @param [Fixnum] | |
# @param [true, false] | |
def rev_stub_words(max_words, save_pos=false) | |
str = self.reverse | |
str.scanner.pos = 0 - scanner.pos if save_pos | |
stub = str.stub_words(max_words, save_pos).reverse! | |
scanner.pos = 0 - str.scanner.pos | |
return stub | |
end | |
def scan_words(scanner, max_words) | |
words = max_words.times.map { | |
scan_word(scanner) | |
} | |
words.compact! | |
words.join | |
end | |
end | |
class String | |
include StringStubber | |
end | |
str = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat." | |
puts str | |
puts str.stub_words(-5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sgonyea@skg:~/tmp$ rvm use ruby-1.8.7 && ruby stub_rbx.rb | |
Using /Users/sgonyea/.rvm/gems/ruby-1.8.7-p334 | |
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat. | |
risus vitae lorem iaculis placerat. | |
sgonyea@skg:~/tmp$ rvm use ruby-1.9.2 && ruby stub_rbx.rb | |
Using /Users/sgonyea/.rvm/gems/ruby-1.9.2-p180 | |
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat. | |
risus vitae lorem iaculis placerat. | |
sgonyea@skg:~/tmp$ rvm use rbx && ruby stub_rbx.rb | |
Using /Users/sgonyea/.rvm/gems/rbx-head | |
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat. | |
tema tis rolod muspi meroL | |
sgonyea@skg:~/tmp$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment