Skip to content

Instantly share code, notes, and snippets.

@digitalmoksha
Created February 19, 2015 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalmoksha/051a00c1448395f1be4a to your computer and use it in GitHub Desktop.
Save digitalmoksha/051a00c1448395f1be4a to your computer and use it in GitHub Desktop.
Attempts to demonstrate a potential issue with regular expressions between Ruby and RubyMotion
# The following code attempts to demonstrate a potential issue with regular
# expressions between Ruby and RubyMotion.
#
# The expression is supposed to match the text before an end of block character (^)
# So
#
# x = Class.new
# ^
#
# should only return the first line, while
#
# header
# =
#
# should return both lines.
#
# Paste this code into a simple ruby file, and both tests should pass.
# Paste into the app_delegate of an OSX RM Application, and the first test fails.
#------------------------------------------------------------------------------
EOB_MARKER = /^\^\s*?\n/
CODEBLOCK_MATCH_2 = /(?:(?!#{EOB_MARKER})^[ \t]*\S.*\n)*/
#------------------------------------------------------------------------------
def regex_match
string = " x = Class.new\n^\n"
matched = CODEBLOCK_MATCH_2.match(string)
correct = " x = Class.new\n"
puts "\nTest 1 - Works in Ruby 1.9.3 and 2.0, fails in RM 3.6"
puts "#------------------------------------------------------------------------------"
puts " string: #{string}"
puts " matched: #{matched[0]}"
puts "should be: #{correct}"
puts matched[0] == correct ? "SUCCESS" : "FAILED"
string = " header\n=\n"
matched = CODEBLOCK_MATCH_2.match(string)
correct = " header\n=\n"
puts "\nTest 2 - Works in Ruby 1.9.3 and 2.0, works in RM 3.6"
puts "#------------------------------------------------------------------------------"
puts " string: #{string}"
puts " matched: #{matched[0]}"
puts "should be: #{correct}"
puts matched[0] == correct ? "SUCCESS" : "FAILED"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment