Skip to content

Instantly share code, notes, and snippets.

@headius
Last active December 15, 2015 18:30
Show Gist options
  • Save headius/5304764 to your computer and use it in GitHub Desktop.
Save headius/5304764 to your computer and use it in GitHub Desktop.
Bad Ruby! No performance for you!
# $~ and $_ are frame-local, but they're set across frames and you don't know when
def foo(a, b)
p $~ # => nil
a[b] # call to String#[], b is a regexp
p $~ # => MatchData
end
foo("hello", /hel/)
# $_ may also be consumed without direct reference
def foo2
gets # will set $_ in caller scope
print # will access $_ in caller scope
end
foo2 # prints out whatever string you type in
def foo3(a)
gets
~a
p $~ # => MatchData
end
foo3(/hel/) # will match against string you type in
# Blocks can be used as bindings when turned into a Proc
def bar
a = 1
baz { puts a } # => 1
puts a # => 2
end
def baz(&block)
eval "a = 2", &block
end
bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment