Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created March 26, 2009 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ELLIOTTCABLE/86009 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/86009 to your computer and use it in GitHub Desktop.
##
# Let's see what we can figure out about Ruby's handling of slashes in weird
# places. Regex, or division?
class Array; def / *args; puts "array #{self.inspect} divided by: " + args.inspect; return args; end; end
class String; def / *args; puts "string #{self.inspect} divided by: " + args.inspect; return args; end; end
def bar *args
args.inspect
end
abc = ["abc"]
puts "1: ============="
p begin
# Here, it's a regex
foo = bar /abc/ # bar(/abc/)
end
puts "2: ============="
p begin
# Here, it's a regex too
foo = bar /abc / # bar(/abc /)
end
puts "3: ============="
p begin
# Here, it's invalid, the first slash is a division, as is hte second.
foo = bar / abc/ # (bar / abc / 123)
123 # Something to divide is necessary!
end
puts "4: ============="
p begin
# Here, it's invalid, the first slash is a division, as is hte second.
foo = bar / abc / # (bar / abc / 123)
123 # Something to divide is necessary!
end
puts "5: ============="
p begin
# Here, it's a division
foo = bar/2 # (bar / 2)
end
puts "6: ============="
p begin
# Here, it's the start of a regex.
foo = bar /2
/ # bar(/2/)
end
puts "7: ============="
p begin
# Here, it's a division
foo = bar / 2 # (bar / 2)
end
puts "8: ============="
p begin
# Regex
foo = /123/
end
puts "9: ============="
p begin
# Regex
foo = /123 /
end
puts "10: ============"
p begin
# Regex
foo = / 123/
end
puts "11: ============"
p begin
# Regex
foo = / 123 /
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment