Skip to content

Commit

Permalink
Only open struct class if there is actually already a class
Browse files Browse the repository at this point in the history
  • Loading branch information
bovi committed Jun 21, 2012
1 parent a686ad6 commit 162b362
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions mrblib/struct.rb
Expand Up @@ -2,45 +2,49 @@
# Struct # Struct
# #
# ISO 15.2.18 # ISO 15.2.18
class Struct


## if Object.const_defined?(:Struct)
# Calls the given block for each element of +self+ class Struct
# and pass the respective element.
#
# ISO 15.2.18.4.4
def each(&block)
self.class.members.each{|field|
block.call(self[field])
}
self
end


## ##
# Calls the given block for each element of +self+ # Calls the given block for each element of +self+
# and pass the name and value of the respectiev # and pass the respective element.
# element. #
# # ISO 15.2.18.4.4
# ISO 15.2.18.4.5 def each(&block)
def each_pair(&block) self.class.members.each{|field|
self.class.members.each{|field| block.call(self[field])
block.call(field.to_sym, self[field]) }
} self
self end
end


## ##
# Calls the given block for each element of +self+ # Calls the given block for each element of +self+
# and returns an array with all elements of which # and pass the name and value of the respectiev
# block is not false. # element.
# #
# ISO 15.2.18.4.7 # ISO 15.2.18.4.5
def select(&block) def each_pair(&block)
ary = [] self.class.members.each{|field|
self.class.members.each{|field| block.call(field.to_sym, self[field])
val = self[field] }
ary.push(val) if block.call(val) self
} end
ary
##
# Calls the given block for each element of +self+
# and returns an array with all elements of which
# block is not false.
#
# ISO 15.2.18.4.7
def select(&block)
ary = []
self.class.members.each{|field|
val = self[field]
ary.push(val) if block.call(val)
}
ary
end
end end
end end

0 comments on commit 162b362

Please sign in to comment.