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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::load::MrbLoadSources;
use crate::Mrb;
use crate::MrbError;
pub fn init(interp: &Mrb) -> Result<(), MrbError> {
interp
.borrow_mut()
.def_module::<Forwardable>("Forwardable", None);
interp.def_rb_source_file("forwardable.rb", include_str!("forwardable.rb"))?;
interp.def_rb_source_file("forwardable/impl.rb", include_str!("forwardable/impl.rb"))?;
Ok(())
}
pub struct Forwardable;
#[cfg(test)]
mod tests {
use crate::eval::MrbEval;
#[test]
#[allow(clippy::shadow_unrelated)]
fn forwardable() {
let interp = crate::interpreter().expect("mrb init");
interp
.eval(
r#"
require 'forwardable'
class RecordCollection
attr_accessor :records
extend Forwardable
def_delegator :@records, :[], :record_number
end
"#,
)
.unwrap();
let result = interp
.eval(
r#"
r = RecordCollection.new
r.records = [4,5,6]
r.record_number(0)
"#,
)
.unwrap()
.try_into::<i64>()
.unwrap();
assert_eq!(result, 4);
interp
.eval(
r#"
class RecordCollection # re-open RecordCollection class
def_delegators :@records, :size, :<<, :map
end
"#,
)
.unwrap();
let result = interp
.eval(
r#"
r = RecordCollection.new
r.records = [1,2,3]
r.record_number(0)
"#,
)
.unwrap()
.try_into::<i64>()
.unwrap();
assert_eq!(result, 1);
let result = interp.eval("r.size").unwrap().try_into::<i64>().unwrap();
assert_eq!(result, 3);
let result = interp
.eval("r << 4")
.unwrap()
.try_into::<Vec<i64>>()
.unwrap();
assert_eq!(result, vec![1, 2, 3, 4]);
let result = interp
.eval("r.map { |x| x * 2 }")
.unwrap()
.try_into::<Vec<i64>>()
.unwrap();
assert_eq!(result, vec![2, 4, 6, 8]);
}
#[test]
fn forwardable_another_example() {
let interp = crate::interpreter().expect("mrb init");
let result = interp
.eval(
r#"
require 'forwardable'
class Queue
extend Forwardable
def initialize
@q = [ ] # prepare delegate object
end
# setup preferred interface, enq() and deq()...
def_delegator :@q, :push, :enq
def_delegator :@q, :shift, :deq
# support some general Array methods that fit Queues well
def_delegators :@q, :clear, :first, :push, :shift, :size
end
out = []
q = Queue.new
q.enq 1, 2, 3, 4, 5
q.push 6
q.shift # => 1
while q.size > 0
out << q.deq.to_s
end
q.enq "Ruby", "Perl", "Python"
out << q.first
q.clear
out << q.first
"#,
)
.unwrap()
.try_into::<Vec<Option<String>>>()
.unwrap();
assert_eq!(
result,
vec![
Some("2".to_owned()),
Some("3".to_owned()),
Some("4".to_owned()),
Some("5".to_owned()),
Some("6".to_owned()),
Some("Ruby".to_owned()),
None
]
);
}
#[test]
fn forwardable_def_instance_delegator() {
let interp = crate::interpreter().expect("mrb init");
let result = interp
.eval(
r#"
require 'forwardable'
class MyQueue
extend Forwardable
attr_reader :queue
def initialize
@queue = []
end
def_delegator :@queue, :push, :mypush
end
q = MyQueue.new
q.mypush 42
# q.queue #=> [42]
raise 'fail' unless q.queue == [42]
# q.push 23 #=> NoMethodError
begin
q.push 23
false
rescue NoMethodError
true
end
"#,
)
.unwrap()
.try_into::<bool>()
.unwrap();
assert!(result);
}
}