-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_rdoc.rb
147 lines (126 loc) · 2.61 KB
/
example_rdoc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
##
# === RDoc::Generator::Markdown example.
#
# This example employs various RDoc features to demonstrate
# generator output.
#
# ---
#
# Links:
#
# 1. {Project Home Page}[https://github.com/skatkov/rdoc-markdown)
# 2. {RDoc Documentation}[http://ruby-doc.org/stdlib-2.0.0/libdoc/rdoc/rdoc/RDoc/Markup.html]
#
##
# A mixin for waterfowl creatures.
module Waterfowl
# Swimming helper.
def swim
puts "swimming around"
end
end
##
# The base class for all birds.
class Bird
##
# Produce some noise.
#--
# FIXME: maybe extract this to a base class +Animal+?
#++
def speak # :yields: text
puts "generic tweeting"
yield "tweet"
yield "tweet"
end
# Fly somewhere.
#
# Flying is the most critical feature of birds.
#
# :args: direction, velocity
#
# :call-seq:
# Bird.fly(symbol, number) -> bool
# Bird.fly(string, number) -> bool
#
# = Example
#
# fly(:south, 70)
def fly(direction, velocity)
_fly_impl(direction, velocity)
end
def _fly_impl(_direction, _velocity) # :nodoc:
puts "flying away: direction=#{direction}, velocity=#{velocity}"
end
end
##
# A duck is a Waterfowl Bird.
#
# Features:
#
# bird::
#
# * speak
# * fly
#
# waterfowl::
#
# * swim
class Duck
extend Animal
include Waterfowl
# :section: Bird overrides
# Duck overrides generic implementation.
def speak
speech = quack
yield speech
end
# Implements quacking
def quack
"quack"
end
private :quack
# :section: Duck extensions
# True for domestic ducks.
attr_accessor :domestic
# True for rubber ducks.
attr_reader :rubber
MAX_VELOCITY = 130 # Maximum velocity for a flying duck.
##
# Global list of all rubber ducks.
#
# Use when in trouble.
@@rubber_ducks = []
# @return [Array<Duck>] list of all rubber ducks
def self.rubber_ducks
@@rubber_ducks
end
# Creates a new duck.
#
# @param [Boolean] domestic
# @param [Boolean] rubber
def initialize(domestic, rubber)
@domestic = domestic
@rubber = rubber
@@rubber_ducks << self if rubber
end
# Checks if this duck is a useful one.
#
# :call-seq:
# Bird.useful? -> bool
def useful?
@domestic || @rubber
end
end
# Default velocity for a flying duck.
DEFAULT_DUCK_VELOCITY = 70
DEFAULT_SPEED = 10 # Maximum speed for a swimming duck.
# Default rubber duck.
#
# *Note:*
# Global variables are evil, but rubber ducks are worth it.
$default_rubber_duck = Duck.new(false, true)
# Domestic rubber duck.
#
# *Note:*
# This is weird... Thus not making it global.
domestic_rubber_duck = Duck.new(true, true) # rubocop:disable Lint/UselessAssignment