Permalink
Browse files

Classes & Syntax

  • Loading branch information...
1 parent e4d4690 commit d70d0dfe220f0a0e97d16e5de3a052156a2202ca @weppos committed Feb 25, 2011
View
@@ -0,0 +1,2 @@
+!SLIDE subsection
+# Classes and Modules
View
@@ -1,18 +1,93 @@
!SLIDE
-# Classes and Modules
-
-- Class / Module
- - attributes
- - methods
- - visibility
- - implicit return
- - namespaces
- - scopeRuby
-
-- Reopening classes
-- Inheritance
-- Mixin
-- Reflection
-
- - send
- - respond_to?
+# Class
+
+
+!SLIDE
+# Class
+
+ @@@ Ruby
+ class Article
+ end
+
+ a = Article.new
+ # => #<Article:0x00000100999f70>
+
+ a.class
+ # => Article
+
+ a.is_a?(Article)
+ # => true
+
+
+!SLIDE
+# Class
+
+ @@@ Ruby
+ class Article
+ def initialize(title)
+ @title = tile
+ end
+
+ def title
+ @title
+ end
+ end
+
+ a = Article.new
+ # => ArgumentError: wrong number of arguments (0 for 1)
+
+ a = Article.new("I'm a Rubyist")
+ # => #<Article:0x00000100999f70>
+
+ p a.title
+ # => "I'm a Rubyist"
+
+
+!SLIDE
+# Class
+
+ @@@ Ruby
+ class Article
+ # Class variable
+ @@year = 2011
+
+ def initialize(title)
+ # Instance variable
+ @title = tile
+ end
+
+ def title
+ "#{@title} (#{@@year})"
+ end
+ end
+
+ a = Article.new("I'm a Rubyist")
+
+ p a.title
+ # => "I'm a Rubyist (2011)"
+
+
+!SLIDE
+# Class
+
+ @@@ Ruby
+ class Base
+ def initialize(title)
+ @title = tile
+ end
+ def title
+ @title.to_s
+ end
+ end
+
+ class Article < Base
+ @@year = 2011
+ def title
+ "#{super} (#{@@year})"
+ end
+ end
+
+ a = Article.new("I'm a Rubyist")
+
+ p a.title
+ # => "I'm a Rubyist (2011)"
View
@@ -0,0 +1,59 @@
+!SLIDE
+# Module
+
+
+!SLIDE
+# Module
+
+ @@@ Ruby
+ module Post
+ end
+
+ p = Post.new
+ # => NoMethodError: undefined method `new' for Post:Module
+
+
+!SLIDE
+# Module
+
+ @@@ Ruby
+ module Post
+ @@year = 2011
+
+ def self.year
+ @@year
+ end
+
+ def title
+ "#{@title} (#{@@year})"
+ end
+ end
+
+ p = Post.year
+ # => 2011
+
+ # How to get instance method #title ?!?
+
+
+!SLIDE
+# Mixin
+
+ @@@ Ruby
+ module Post
+ @@year = 2011
+
+ def title
+ "#{@title} (#{@@year})"
+ end
+ end
+
+ class Article
+ include Post # <- Include the module Post
+
+ def initialize(title)
+ @title = title
+ end
+ end
+
+ Article.new("I'm a Rubyist").title
+ # => "I'm a Rubyist (2011)"
Oops, something went wrong.

0 comments on commit d70d0df

Please sign in to comment.