Permalink
Browse files

add accessors

  • Loading branch information...
lgmkr committed Oct 2, 2011
1 parent 888a292 commit ca1c44593f93d0be647ac653330f790bdda8fbf2
Showing with 49 additions and 0 deletions.
  1. +11 −0 accessors/accessors_01.rb
  2. +11 −0 accessors/accessors_02.rb
  3. +17 −0 accessors/accessors_03.rb
  4. +10 −0 accessors/accessors_04.rb
View
@@ -0,0 +1,11 @@
+class Post
+ @title = "hello"
+end
+
+puts "--- play with Post class"
+p Post.title
+
+
+
+
+
View
@@ -0,0 +1,11 @@
+class Post
+ def title #getter
+ p @title.class
+ end
+end
+
+puts "--- play with Post class"
+
+p = Post.new
+p.title
+p.title = "bye"
View
@@ -0,0 +1,17 @@
+class Post
+
+ def title #getter
+ @title
+ end
+
+ def title= (value) #setter
+ @title = value
+ end
+
+end
+
+puts "--- play with Post class"
+
+p = Post.new
+p.title = "bye"
+p p.title
View
@@ -0,0 +1,10 @@
+class Post
+ attr_accessor :title, :language
+end
+
+p = Post.new
+p.title = 'hello world' #call setter method
+puts p.title #call getter method
+
+p.language = 'russian' #call setter method
+puts p.language #call getter method

0 comments on commit ca1c445

Please sign in to comment.