Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

isinstance and issubclass

>>> class P(object):  # parent class
	pass

>>> class K(P):       # subclass
	pass

>>> k = K()           # instace
>>> isinstance(k, K)
True
>>> isinstance(k, P)
True
>>> isinstance(K, P)  # K is a class
False

>>> issubclass(K, P)
True
>>> issubclass(k, P)  # k is not a class

TypeError: issubclass() arg 1 must be a class

>>> isinstance(K, type)  # a class is an instace of type
True
>>> isinstance(k, type)  # not a class
False

Fix for nil object error in Rails test fixtures

If you're seeing errors like this when you run Rails tests:

# NoMethodError: You have a nil object when you didn't expect it!


You might need to edit test/test_helper.rb to make sure use_instantiated_fixtures is true:

self.use_instantiated_fixtures = true


Prior to 1.0, Rails automatically created instance variables out of fixtures. So if you had a fixture record named "foo", you could access it in your test as "@foo". As of 1.0, the default is to disable that feature, which breaks a lot of existing code. Mike Clark explains the change.

parameterized instance variables

// useful when you want to obtain more than one instance of the same type of variable on the
// same page. i.e. two different addresses. credit goes to argv[0] in #rubyonrails on freenode

instance_variable_get("@#{address_type}_address").country.name
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS