Permalink
Browse files

Merge pull request #77 from iainbeeston/patch-1

Showed how to access class variables from an instance method
  • Loading branch information...
2 parents 38dc427 + 57c445a commit 653016fd7156bf7805f3961fb0aa67711b6ecd38 @peterhellberg peterhellberg committed Apr 9, 2013
Showing with 5 additions and 0 deletions.
  1. +5 −0 chapters/classes_and_objects/class-variables.md
@@ -13,6 +13,9 @@ You want to create a class variable.
class Zoo
@MAX_ANIMALS: 50
MAX_ZOOKEEPERS: 3
+
+ helpfulInfo: =>
+ "Zoos may contain a maximum of #{@constructor.MAX_ANIMALS} animals"
@sukima
sukima Apr 9, 2013 Contributor

I don't think I've ever used @constructor before. Is this something special? I've always referenced an @static variable with the name of the class (Zoo.MAX_ANIMALS). What are the pros and con of the two styles?

@peterhellberg
peterhellberg Apr 9, 2013 Member

One of the pros is that you don’t need to hardcode the name of the class. I’ve seen some discussion going on for a shorthand to @constructor (possibly @@) but I don’t think there is any consensus on that yet.

@iainbeeston
iainbeeston via email Apr 9, 2013 Member
@sukima
sukima Apr 9, 2013 Contributor

I know arguing semantic now but I like the class name syntax better. It seems more readable. Then again my class names rarely change and when they do a s/ClassName/NewName/g does the trick.

Maybe it would be good to offer both syntaxes for readers.

@iainbeeston
iainbeeston via email Apr 9, 2013 Member
@iainbeeston
iainbeeston via email Apr 9, 2013 Member
@dbrady
dbrady via email Apr 12, 2013 Member
@sukima
sukima Apr 12, 2013 Contributor

My turn to pipe in. So I'm of the opinion that a recipe is a spot to solve a problem and offer a best practices solution. However I, and many others, have come to this book/site as a way to augment the base documentation and see examples of more advanced programming techniques.

So I propose we offer problem/best practice solutions in our recipes but also have a section for great tips and tricks.

Zoo.MAX_ANIMALS
# => 50
@@ -23,6 +26,8 @@ Zoo.MAX_ZOOKEEPERS
zoo = new Zoo
zoo.MAX_ZOOKEEPERS
# => 3
+zoo.helpfulInfo()
+# => "Zoos may contain a maximum of 50 animals"
{% endhighlight %}
## Discussion

0 comments on commit 653016f

Please sign in to comment.