Class: Struct
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Enumerable
#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find_all, #find_index, #first, #grep, #group_by, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip
Constructor Details
#initialize(*args) ⇒ Struct
Returns a new instance of Struct
58
59
60
61
62
|
# File 'opal/opal/corelib/struct.rb', line 58
def initialize(*args)
members.each_with_index {|name, index|
instance_variable_set "@#{name}", args[index]
}
end
|
Class Method Details
.define_struct_attribute(name) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'opal/opal/corelib/struct.rb', line 18
def self.define_struct_attribute(name)
if self == Struct
raise ArgumentError, 'you cannot define attributes to the Struct class'
end
members << name
define_method name do
instance_variable_get "@#{name}"
end
define_method "#{name}=" do |value|
instance_variable_set "@#{name}", value
end
end
|
.inherited(klass) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'opal/opal/corelib/struct.rb', line 42
def self.inherited(klass)
return if self == Struct
members = @members
klass.instance_eval {
@members = members
}
end
|
.members ⇒ Object
34
35
36
37
38
39
40
|
# File 'opal/opal/corelib/struct.rb', line 34
def self.members
if self == Struct
raise ArgumentError, 'the Struct class has no members'
end
@members ||= []
end
|
.new(name = undefined, *args, &block) ⇒ Object
Also known as:
[]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'opal/opal/corelib/struct.rb', line 2
def self.new(name = undefined, *args, &block)
return super unless self == Struct
if name[0] == name[0].upcase
Struct.const_set(name, new(*args))
else
args.unshift name
Class.new(self) {
args.each { |arg| define_struct_attribute arg }
instance_eval(&block) if block
}
end
end
|
Instance Method Details
#[](name) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'opal/opal/corelib/struct.rb', line 68
def [](name)
if Integer === name
raise IndexError, "offset #{name} too large for struct(size:#{members.size})" if name >= members.size
name = members[name]
else
raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
end
instance_variable_get "@#{name}"
end
|
#[]=(name, value) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'opal/opal/corelib/struct.rb', line 80
def []=(name, value)
if Integer === name
raise IndexError, "offset #{name} too large for struct(size:#{members.size})" if name >= members.size
name = members[name]
else
raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
end
instance_variable_set "@#{name}", value
end
|
#each ⇒ Object
92
93
94
95
96
97
|
# File 'opal/opal/corelib/struct.rb', line 92
def each
return enum_for :each unless block_given?
members.each { |name| yield self[name] }
self
end
|
#each_pair ⇒ Object
99
100
101
102
103
104
|
# File 'opal/opal/corelib/struct.rb', line 99
def each_pair
return enum_for :each_pair unless block_given?
members.each { |name| yield name, self[name] }
self
end
|
#eql?(other) ⇒ Boolean
106
107
108
109
110
|
# File 'opal/opal/corelib/struct.rb', line 106
def eql?(other)
hash == other.hash || other.each_with_index.all? {|object, index|
self[members[index]] == object
}
end
|
#inspect ⇒ Object
Also known as:
to_s
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'opal/opal/corelib/struct.rb', line 124
def inspect
result = "#<struct "
if self.class == Struct
result += "#{self.class.name} "
end
result += each_pair.map {|name, value|
"#{name}=#{value.inspect}"
}.join ", "
result += ">"
result
end
|
#length ⇒ Object
Also known as:
size
112
113
114
|
# File 'opal/opal/corelib/struct.rb', line 112
def length
members.length
end
|
#members ⇒ Object
64
65
66
|
# File 'opal/opal/corelib/struct.rb', line 64
def members
self.class.members
end
|
#to_a ⇒ Object
Also known as:
values
118
119
120
|
# File 'opal/opal/corelib/struct.rb', line 118
def to_a
members.map { |name| self[name] }
end
|