Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add accessors

  • Loading branch information...
commit ca1c44593f93d0be647ac653330f790bdda8fbf2 1 parent 888a292
@stancox authored
View
11 accessors/accessors_01.rb
@@ -0,0 +1,11 @@
+class Post
+ @title = "hello"
+end
+
+puts "--- play with Post class"
+p Post.title
+
+
+
+
+
View
11 accessors/accessors_02.rb
@@ -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
17 accessors/accessors_03.rb
@@ -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
10 accessors/accessors_04.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.