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
52
53
54
55
56
|
# File 'opal/opal/corelib/struct.rb', line 52
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
|
# 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
attr_accessor name
end
|
.inherited(klass) ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'opal/opal/corelib/struct.rb', line 36
def self.inherited(klass)
return if self == Struct
members = @members
klass.instance_eval {
@members = members
}
end
|
.members ⇒ Object
28
29
30
31
32
33
34
|
# File 'opal/opal/corelib/struct.rb', line 28
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
62
63
64
65
66
67
68
69
70
71
72
|
# File 'opal/opal/corelib/struct.rb', line 62
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
74
75
76
77
78
79
80
81
82
83
84
|
# File 'opal/opal/corelib/struct.rb', line 74
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
86
87
88
89
90
91
|
# File 'opal/opal/corelib/struct.rb', line 86
def each
return enum_for :each unless block_given?
members.each { |name| yield self[name] }
self
end
|
#each_pair ⇒ Object
93
94
95
96
97
98
|
# File 'opal/opal/corelib/struct.rb', line 93
def each_pair
return enum_for :each_pair unless block_given?
members.each { |name| yield name, self[name] }
self
end
|
#eql?(other) ⇒ Boolean
100
101
102
103
104
|
# File 'opal/opal/corelib/struct.rb', line 100
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'opal/opal/corelib/struct.rb', line 118
def inspect
result = "#<struct "
if self.class == Struct
result += "#{self.class} "
end
result += each_pair.map {|name, value|
"#{name}=#{value.inspect}"
}.join ", "
result += ">"
result
end
|
#length ⇒ Object
Also known as:
size
106
107
108
|
# File 'opal/opal/corelib/struct.rb', line 106
def length
members.length
end
|
#members ⇒ Object
58
59
60
|
# File 'opal/opal/corelib/struct.rb', line 58
def members
self.class.members
end
|
#to_a ⇒ Object
Also known as:
values
112
113
114
|
# File 'opal/opal/corelib/struct.rb', line 112
def to_a
members.map { |name| self[name] }
end
|