public
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
##
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.