Skip to content

Instantly share code, notes, and snippets.

@foca
Created February 10, 2011 19:07
Show Gist options
  • Save foca/821116 to your computer and use it in GitHub Desktop.
Save foca/821116 to your computer and use it in GitHub Desktop.
This is totally evil.
o1 = Object.new
o2 = Object.new
def (1 > 2 ? o1 : o2).blah
puts "blah"
end
o2.blah #=> puts "blah"
o1.blah #=> NoMethodError
@foca
Copy link
Author

foca commented Feb 10, 2011

So python also supports this (via @tiodante)

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
...     pass
... 
>>> a1 = A()
>>> a2 = A()
>>> (a1 if 1 > 2 else a2).test = lambda: "test"
>>> a2.test()
'test'
>>> a1.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'test'
>>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment