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
d35677f2
Unverified
Commit
d35677f2
authored
Oct 16, 2020
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement `Range#last` in `mruby-range-ext` in Ruby.
This reduce unnecessary calls of `mrb_funcall()`.
parent
76eafa46
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
36 deletions
+26
-36
mrbgems/mruby-range-ext/mrblib/range.rb
mrbgems/mruby-range-ext/mrblib/range.rb
+26
-0
mrbgems/mruby-range-ext/src/range.c
mrbgems/mruby-range-ext/src/range.c
+0
-36
No files found.
mrbgems/mruby-range-ext/mrblib/range.rb
View file @
d35677f2
...
...
@@ -26,6 +26,32 @@ class Range
ary
end
##
# call-seq:
# rng.last -> obj
# rng.last(n) -> an_array
#
# Returns the last object in the range,
# or an array of the last +n+ elements.
#
# Note that with no arguments +last+ will return the object that defines
# the end of the range even if #exclude_end? is +true+.
#
# (10..20).last #=> 20
# (10...20).last #=> 20
# (10..20).last(3) #=> [18, 19, 20]
# (10...20).last(3) #=> [17, 18, 19]
def
last
(
*
args
)
raise
RangeError
,
"cannot get the first element of beginless range"
if
self
.
end
.
nil?
return
self
.
end
if
args
.
empty?
raise
ArgumentError
,
"wrong number of arguments (given
#{
args
.
length
}
, expected 1)"
unless
args
.
length
==
1
nv
=
args
[
0
]
n
=
nv
.
__to_int
raise
ArgumentError
,
"negative array size (or size too big)"
unless
0
<=
n
return
self
.
to_a
.
last
(
nv
)
end
def
max
(
&
block
)
val
=
self
.
begin
last
=
self
.
end
...
...
mrbgems/mruby-range-ext/src/range.c
View file @
d35677f2
...
...
@@ -58,41 +58,6 @@ range_cover(mrb_state *mrb, mrb_value range)
return
mrb_false_value
();
}
/*
* call-seq:
* rng.last -> obj
* rng.last(n) -> an_array
*
* Returns the last object in the range,
* or an array of the last +n+ elements.
*
* Note that with no arguments +last+ will return the object that defines
* the end of the range even if #exclude_end? is +true+.
*
* (10..20).last #=> 20
* (10...20).last #=> 20
* (10..20).last(3) #=> [18, 19, 20]
* (10...20).last(3) #=> [17, 18, 19]
*/
static
mrb_value
range_last
(
mrb_state
*
mrb
,
mrb_value
range
)
{
mrb_value
num
;
mrb_value
array
;
struct
RRange
*
r
=
mrb_range_ptr
(
mrb
,
range
);
if
(
mrb_nil_p
(
RANGE_END
(
r
)))
{
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
"cannot get the last element of endless range"
);
}
if
(
mrb_get_args
(
mrb
,
"|o"
,
&
num
)
==
0
)
{
return
mrb_range_end
(
mrb
,
range
);
}
array
=
mrb_funcall_id
(
mrb
,
range
,
MRB_SYM
(
to_a
),
0
);
return
mrb_funcall_id
(
mrb
,
array
,
MRB_SYM
(
last
),
1
,
mrb_to_int
(
mrb
,
num
));
}
/*
* call-seq:
* rng.size -> num
...
...
@@ -194,7 +159,6 @@ mrb_mruby_range_ext_gem_init(mrb_state* mrb)
struct
RClass
*
s
=
mrb_class_get
(
mrb
,
"Range"
);
mrb_define_method
(
mrb
,
s
,
"cover?"
,
range_cover
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
s
,
"last"
,
range_last
,
MRB_ARGS_OPT
(
1
));
mrb_define_method
(
mrb
,
s
,
"size"
,
range_size
,
MRB_ARGS_NONE
());
}
...
...
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