Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
6d0b5102
Unverified
Commit
6d0b5102
authored
Feb 06, 2021
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Feb 06, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5326 from dearblue/struct
No need to check class definition
parents
88113df0
0f8d3d87
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
84 deletions
+81
-84
mrbgems/mruby-struct/mrblib/struct.rb
mrbgems/mruby-struct/mrblib/struct.rb
+81
-84
No files found.
mrbgems/mruby-struct/mrblib/struct.rb
View file @
6d0b5102
...
...
@@ -2,99 +2,96 @@
# Struct
#
# ISO 15.2.18
class
Struct
if
Object
.
const_defined?
(
:Struct
)
class
Struct
##
# Calls the given block for each element of +self+
# 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+
# 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+
# and pass the name and value of the respective
# element.
#
# ISO 15.2.18.4.5
def
each_pair
(
&
block
)
self
.
class
.
members
.
each
{
|
field
|
block
.
call
(
field
.
to_sym
,
self
[
field
])
}
self
end
##
# Calls the given block for each element of +self+
# and pass the name and value of the respective
# element.
#
# ISO 15.2.18.4.5
def
each_pair
(
&
block
)
self
.
class
.
members
.
each
{
|
field
|
block
.
call
(
field
.
to_sym
,
self
[
field
])
}
self
end
##
# 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
##
# 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
def
_inspect
(
recur_list
)
return
"#<struct
#{
self
.
class
}
:...>"
if
recur_list
[
self
.
object_id
]
recur_list
[
self
.
object_id
]
=
true
name
=
self
.
class
.
to_s
if
name
[
0
]
==
"#"
str
=
"#<struct "
else
str
=
"#<struct
#{
name
}
"
end
def
_inspect
(
recur_list
)
return
"#<struct
#{
self
.
class
}
:...>"
if
recur_list
[
self
.
object_id
]
recur_list
[
self
.
object_id
]
=
true
name
=
self
.
class
.
to_s
if
name
[
0
]
==
"#"
str
=
"#<struct "
else
str
=
"#<struct
#{
name
}
"
end
buf
=
[]
self
.
each_pair
do
|
k
,
v
|
buf
.
push
k
.
to_s
+
"="
+
v
.
_inspect
(
recur_list
)
end
str
+
buf
.
join
(
", "
)
+
">"
buf
=
[]
self
.
each_pair
do
|
k
,
v
|
buf
.
push
k
.
to_s
+
"="
+
v
.
_inspect
(
recur_list
)
end
str
+
buf
.
join
(
", "
)
+
">"
end
##
# call-seq:
# struct.to_s -> string
# struct.inspect -> string
#
# Describe the contents of this struct in a string.
#
# 15.2.18.4.10(x)
#
def
inspect
self
.
_inspect
({})
end
##
# call-seq:
# struct.to_s -> string
# struct.inspect -> string
#
# Describe the contents of this struct in a string.
#
# 15.2.18.4.10(x)
#
def
inspect
self
.
_inspect
({})
end
##
# 15.2.18.4.11(x)
#
alias
to_s
inspect
##
# 15.2.18.4.11(x)
#
alias
to_s
inspect
##
# call-seq:
# hsh.dig(key,...) -> object
#
# Extracts the nested value specified by the sequence of <i>key</i>
# objects by calling +dig+ at each step, returning +nil+ if any
# intermediate step is +nil+.
#
def
dig
(
idx
,
*
args
)
n
=
self
[
idx
]
if
args
.
size
>
0
n
&
.
dig
(
*
args
)
else
n
end
##
# call-seq:
# hsh.dig(key,...) -> object
#
# Extracts the nested value specified by the sequence of <i>key</i>
# objects by calling +dig+ at each step, returning +nil+ if any
# intermediate step is +nil+.
#
def
dig
(
idx
,
*
args
)
n
=
self
[
idx
]
if
args
.
size
>
0
n
&
.
dig
(
*
args
)
else
n
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment