summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Bard <github@adambard.com>2013-08-07 11:00:24 -0700
committerAdam Bard <github@adambard.com>2013-08-07 11:00:24 -0700
commit896c6cfc93972104b354dd92e42c0b4572b04555 (patch)
tree7f921a28567623e9e97f9f4eacf3421599a1b047
parentd55031937d48ec5d853e149ea2f122d645c0eeb4 (diff)
parent954fa45acfc400bee8cb571bf4367497ca207d2a (diff)
Merge pull request #166 from greybird/master
Add Ruby
-rw-r--r--ruby.html.markdown46
1 files changed, 46 insertions, 0 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown
index a3bcbbd5..99817982 100644
--- a/ruby.html.markdown
+++ b/ruby.html.markdown
@@ -325,4 +325,50 @@ dwight.name #=> "Dwight K. Schrute"
# Call the class method
Human.say("Hi") #=> "Hi"
+# Class also is object in ruby. So class can have instance variables.
+# Class variable is shared among the class and all of its descendants.
+
+# base class
+class Human
+ @@foo = 0
+
+ def self.foo
+ @@foo
+ end
+
+ def self.foo=(value)
+ @@foo = value
+ end
+end
+
+# derived class
+class Worker < Human
+end
+
+Human.foo # 0
+Worker.foo # 0
+
+Human.foo = 2 # 2
+Worker.foo # 2
+
+# Class instance variable is not shared by the class's descendants.
+
+class Human
+ @bar = 0
+
+ def self.bar
+ @bar
+ end
+
+ def self.bar=(value)
+ @bar = value
+ end
+end
+
+class Doctor < Human
+end
+
+Human.bar # 0
+Doctor.bar # nil
+
```