Ruby bits (class 篇)


Outline
  1. 1. 目錄 (class 篇)
  2. 2. 1. Scope $: global、@@: class、@: instance
  3. 3. 2. attr_accessor (getter & setter)
    1. 3.1. same as
    2. 3.2. attr_reader、attr_writer、self
  4. 4. 3. private(cannot be called with explicit receiver)
    1. 4.1. protected (可被 class 內呼叫)
  5. 5. 4. Inheritance( Override、super)
  6. 6. 5. ACTIVESUPPORT
  7. 7. 6. Struct
  8. 8. 7. Send(可以呼叫 private or protected method)
  9. 9. 8. method (放在記憶體,等等再 .call )

目錄 (class 篇)

  1. Scope
  2. attr_accessor、attr_reader、self
  3. private、protected
  4. Inheritance
  5. ACTIVESUPPORT
  6. Struct
  7. Send
  8. method

1. Scope $: global@@: class@: instance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Computer
$manufacturer = "Mango Computer, Inc." # global variable
@@files = {hello: "Hello, world!"} # class variable
def initialize(username, password)
@username = username # instance variable
@password = password
end
def current_user
@username
end
def self.display_files # class method
@@files
end
end
1
2
3
4
5
hal = Computer.new("Dave", 12345)
puts "Current user: #{hal.current_user}" #=> Current user: Dave
puts "Manufacturer: #{$manufacturer}" #=> Manufacturer: Mango Computer, Inc.
puts "Files: #{Computer.display_files}" #=> Files: {:hello=>"Hello, world!"}

2. attr_accessor (getter & setter)

1
attr_accessor :baz

same as

1
2
3
4
5
6
7
def baz=(value)
@baz = value
end
def baz
@baz
end

attr_reader、attr_writer、self

1
2
3
4
5
6
7
8
class Tweet
attr_accessor :status
attr_reader :created_at # 沒有setter
def initialize(status)
self.status = status # 或 @status = status
@created_at = Time.new # 不能 self.created_at
end
end

self: the current object,需要 setter。

1
2
3
4
5
6
7
8
class Person
attr_reader :name # getting
attr_writer :job # changing
def initialize(name, job)
@name = name
@job = job
end
end
  • attr_reader + attr_writer = attr_accessor

3. private(cannot be called with explicit receiver)

  • 預設是 public
1
2
3
4
5
6
7
8
9
10
11
12
class User
def up_vote(friend)
bump_karma # 能呼叫
friend.bump_karma # 不能呼叫(外來物件)
end
private
def bump_karma
puts "karma up for #{name}"
end
end

protected (可被 class 內呼叫)

1
2
3
4
5
6
7
8
9
10
11
12
class User
def up_vote(friend)
bump_karma # 能呼叫
friend.bump_karma # 能呼叫
end
protected
def bump_karma
puts "karma up for #{name}"
end
end

4. Inheritance( Override、super

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Creature
def initialize(name)
@name = name
end
def fight
return "B"
end
end
class Dragon < Creature
def fight # Override
puts "A"
super # super
end
end
1
2
3
=>
A
B

5. ACTIVESUPPORT

1
2
3
4
5
6
# install
$ gem install activesupport
$ gem install i18n
# load it
require 'active_support/all'

6. Struct

1
2
3
4
5
6
7
8
9
class Tweet
attr_accessor :user, :status
def initialize(user, status)
@user, @status = user, status
end
def to_s
"#{user}: #{status}"
end
end

等價於

1
2
3
4
5
Tweet = Struct.new(:user, :status) do
def to_s
"#{user}: #{status}"
end
end

7. Send(可以呼叫 private or protected method)

1
2
3
4
5
6
7
8
9
10
11
12
class Timeline
def initialize(tweets)
@tweets = tweets
end
def contents
@tweets
end
private
def direct_messages
end
end
1
2
3
4
5
6
7
8
9
10
tweets = ['Compiling!', 'Bundling...']
timeline = Timeline.new(tweets)
timeline.contents
等價於
timeline.send(:contents)
等價於
timeline.send("contents")
timeline.send(:direct_messages) # call private

8. method (放在記憶體,等等再 .call

1
2
3
4
5
6
7
8
class Timeline
def initialize(tweets)
@tweets = tweets
end
def contents
@tweets
end
end
1
2
3
4
content_method = timeline.method(:contents)
#=> #<Method: Timeline#contents>
content_method.call
#=> ["Compiling!", "Bundling..."]