Commit 5cdcce8d authored by Seba Gamboa's avatar Seba Gamboa

Mark core gems with mrbgem tag

parent 84b70886
......@@ -32,12 +32,34 @@ MRB_API mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_va
/* declaration for fail method */
MRB_API mrb_value mrb_f_raise(mrb_state*, mrb_value);
/* functions defined in mruby-error mrbgem */
/**
* Protect
*
* @mrbgem mruby-error
*/
MRB_API mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state);
/**
* Ensure
*
* @mrbgem mruby-error
*/
MRB_API mrb_value mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
mrb_func_t ensure, mrb_value e_data);
/**
* Rescue
*
* @mrbgem mruby-error
*/
MRB_API mrb_value mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
mrb_func_t rescue, mrb_value r_data);
/**
* Rescue exception
*
* @mrbgem mruby-error
*/
MRB_API mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
mrb_func_t rescue, mrb_value r_data,
mrb_int len, struct RClass **classes);
......
class Array
##
# call-seq:
# ary.uniq! -> ary or nil
......@@ -15,6 +16,7 @@ class Array
# c = [["student","sam"], ["student","george"], ["teacher","matz"]]
# c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
#
# @mrbgem mruby-array-ext
def uniq!(&block)
ary = self.dup
result = []
......@@ -54,6 +56,7 @@ class Array
# b = [["student","sam"], ["student","george"], ["teacher","matz"]]
# b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
#
# @mrbgem mruby-array-ext
def uniq(&block)
ary = self.dup
if block
......@@ -75,6 +78,7 @@ class Array
#
# [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
#
# @mrbgem mruby-array-ext
def -(elem)
raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
......@@ -95,6 +99,7 @@ class Array
# [ "a", "b", "c" ] | [ "c", "d", "a" ]
# #=> [ "a", "b", "c", "d" ]
#
# @mrbgem mruby-array-ext
def |(elem)
raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
......@@ -111,6 +116,7 @@ class Array
#
# [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
#
# @mrbgem mruby-array-ext
def &(elem)
raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
......@@ -143,6 +149,7 @@ class Array
# a = [ 1, 2, [3, [4, 5] ] ]
# a.flatten(1) #=> [1, 2, 3, [4, 5]]
#
# @mrbgem mruby-array-ext
def flatten(depth=nil)
ar = []
self.each do |e|
......@@ -172,6 +179,7 @@ class Array
# a = [ 1, 2, [3, [4, 5] ] ]
# a.flatten!(1) #=> [1, 2, 3, [4, 5]]
#
# @mrbgem mruby-array-ext
def flatten!(depth=nil)
modified = false
ar = []
......@@ -199,6 +207,7 @@ class Array
# [ "a", nil, "b", nil, "c", nil ].compact
# #=> [ "a", "b", "c" ]
#
# @mrbgem mruby-array-ext
def compact
result = self.dup
result.compact!
......@@ -216,6 +225,7 @@ class Array
# [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
# [ "a", "b", "c" ].compact! #=> nil
#
# @mrbgem mruby-array-ext
def compact!
result = self.select { |e| !e.nil? }
if result.size == self.size
......@@ -226,6 +236,7 @@ class Array
end
# for efficiency
# @mrbgem mruby-array-ext
def reverse_each(&block)
return to_enum :reverse_each unless block_given?
......@@ -238,6 +249,7 @@ class Array
end
NONE=Object.new
##
# call-seq:
# ary.fetch(index) -> obj
......@@ -260,7 +272,7 @@ class Array
# a.fetch(100) { |i| puts "#{i} is out of bounds" }
# #=> "100 is out of bounds"
#
# @mrbgem mruby-array-ext
def fetch(n=nil, ifnone=NONE, &block)
warn "block supersedes default value argument" if !n.nil? && ifnone != NONE && block
......@@ -310,7 +322,7 @@ class Array
# a.fill(1, 2) { |i| i+1 } #=> [0, 2, 3, 27]
# a.fill(0..1) { |i| i+1 } #=> [1, 2, 3, 27]
#
# @mrbgem mruby-array-ext
def fill(arg0=nil, arg1=nil, arg2=nil, &block)
if arg0.nil? && arg1.nil? && arg2.nil? && !block
raise ArgumentError, "wrong number of arguments (0 for 1..3)"
......@@ -394,7 +406,8 @@ class Array
# a #=> ["a", "b", "c", "d"]
# a.rotate(2) #=> ["c", "d", "a", "b"]
# a.rotate(-3) #=> ["b", "c", "d", "a"]
#
# @mrbgem mruby-array-ext
def rotate(count=1)
ary = []
len = self.length
......@@ -425,12 +438,12 @@ class Array
# a #=> ["b", "c", "d", "a"]
# a.rotate!(2) #=> ["d", "a", "b", "c"]
# a.rotate!(-3) #=> ["a", "b", "c", "d"]
#
# @mrbgem mruby-array-ext
def rotate!(count=1)
self.replace(self.rotate(count))
end
##
# call-seq:
# ary.delete_if { |item| block } -> ary
# ary.delete_if -> Enumerator
......@@ -446,7 +459,8 @@ class Array
#
# scores = [ 97, 42, 75 ]
# scores.delete_if {|score| score < 80 } #=> [97]
#
# @mrbgem mruby-array-ext
def delete_if(&block)
return to_enum :delete_if unless block_given?
......@@ -475,7 +489,8 @@ class Array
# See also Enumerable#reject and Array#delete_if.
#
# If no block is given, an Enumerator is returned instead.
#
# @mrbgem mruby-array-ext
def reject!(&block)
return to_enum :reject! unless block_given?
......@@ -507,7 +522,8 @@ class Array
# a = %w{ a b c d }
# a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
# a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
#
# @mrbgem mruby-array-ext
def insert(idx, *args)
idx += self.size + 1 if idx < 0
self[idx, 0] = args
......@@ -565,7 +581,8 @@ class Array
# You must not mix the two modes at a time; the block must always
# return either true/false, or always return a number. It is
# undefined which value is actually picked up at each iteration.
#
# @mrbgem mruby-array-ext
def bsearch(&block)
return to_enum :bsearch unless block_given?
......@@ -612,7 +629,8 @@ class Array
#
# scores = [ 97, 42, 75 ]
# scores.delete_if {|score| score < 80 } #=> [97]
#
# @mrbgem mruby-array-ext
def delete_if(&block)
return to_enum :delete_if unless block_given?
......@@ -641,7 +659,8 @@ class Array
#
# a = [1, 2, 3, 4, 5]
# a.keep_if { |val| val > 3 } #=> [4, 5]
#
# @mrbgem mruby-array-ext
def keep_if(&block)
return to_enum :keep_if unless block_given?
......@@ -670,7 +689,8 @@ class Array
# See also Array#keep_if
#
# If no block is given, an Enumerator is returned instead.
#
# @mrbgem mruby-array-ext
def select!(&block)
return to_enum :select! unless block_given?
......@@ -694,6 +714,8 @@ class Array
# first object for which the block returns +true+. Returns +nil+ if no
# match is found.
#
# @mrbgem mruby-array-ext
#
# ISO 15.2.12.5.14
def index(val=NONE, &block)
return to_enum(:find_index, val) if !block && val == NONE
......
......@@ -4,7 +4,7 @@
#include "mruby/range.h"
#include "mruby/hash.h"
/*
/**
* call-seq:
* ary.assoc(obj) -> new_ary or nil
*
......@@ -22,8 +22,9 @@
* a = [ s1, s2, s3 ]
* a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
* a.assoc("foo") #=> nil
*
* @mrbgem mruby-array-ext
*/
static mrb_value
mrb_ary_assoc(mrb_state *mrb, mrb_value ary)
{
......@@ -73,7 +74,7 @@ mrb_ary_rassoc(mrb_state *mrb, mrb_value ary)
return mrb_nil_value();
}
/*
/**
* call-seq:
* ary.at(index) -> obj or nil
*
......@@ -84,8 +85,9 @@ mrb_ary_rassoc(mrb_state *mrb, mrb_value ary)
* a = [ "a", "b", "c", "d", "e" ]
* a.at(0) #=> "a"
* a.at(-1) #=> "e"
*
* @mrbgem mruby-array-ext
*/
static mrb_value
mrb_ary_at(mrb_state *mrb, mrb_value ary)
{
......@@ -95,6 +97,10 @@ mrb_ary_at(mrb_state *mrb, mrb_value ary)
return mrb_ary_entry(ary, pos);
}
/**
*
* @mrbgem mruby-array-ext
*/
static mrb_value
mrb_ary_values_at(mrb_state *mrb, mrb_value self)
{
......@@ -106,7 +112,7 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self)
return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref);
}
/*
/**
* call-seq:
* ary.to_h -> Hash
*
......@@ -115,8 +121,9 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self)
*
* [[:foo, :bar], [1, 2]].to_h
* # => {:foo => :bar, 1 => 2}
*
* @mrbgem mruby-array-ext
*/
static mrb_value
mrb_ary_to_h(mrb_state *mrb, mrb_value ary)
{
......
##
# Enumerable
#
module Enumerable
##
# call-seq:
# enum.drop(n) -> array
......@@ -11,7 +9,8 @@ module Enumerable
#
# a = [1, 2, 3, 4, 5, 0]
# a.drop(3) #=> [4, 5, 0]
#
# @mrbgem mruby-enum-ext
def drop(n)
raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "attempt to drop negative size" if n < 0
......@@ -35,7 +34,8 @@ module Enumerable
#
# a = [1, 2, 3, 4, 5, 0]
# a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
#
# @mrbgem mruby-enum-ext
def drop_while(&block)
return to_enum :drop_while unless block
......@@ -55,7 +55,8 @@ module Enumerable
#
# a = [1, 2, 3, 4, 5, 0]
# a.take(3) #=> [1, 2, 3]
#
# @mrbgem mruby-enum-ext
def take(n)
raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "attempt to take negative size" if n < 0
......@@ -81,7 +82,8 @@ module Enumerable
#
# a = [1, 2, 3, 4, 5, 0]
# a.take_while {|i| i < 3 } #=> [1, 2]
#
# @mrbgem mruby-enum-ext
def take_while(&block)
return to_enum :take_while unless block
......@@ -111,7 +113,8 @@ module Enumerable
# [6, 7, 8]
# [7, 8, 9]
# [8, 9, 10]
#
# @mrbgem mruby-enum-ext
def each_cons(n, &block)
raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "invalid size" if n <= 0
......@@ -139,7 +142,8 @@ module Enumerable
# [4, 5, 6]
# [7, 8, 9]
# [10]
#
# @mrbgem mruby-enum-ext
def each_slice(n, &block)
raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "invalid slice size" if n <= 0
......@@ -167,7 +171,8 @@ module Enumerable
# corresponding to the key.
#
# (1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
#
# @mrbgem mruby-enum-ext
def group_by(&block)
return to_enum :group_by unless block
......@@ -189,7 +194,8 @@ module Enumerable
# values in <i>enum</i> through the given block.
#
# If no block is given, an enumerator is returned instead.
#
# @mrbgem mruby-enum-ext
def sort_by(&block)
return to_enum :sort_by unless block
......@@ -216,6 +222,8 @@ module Enumerable
# Returns the first element, or the first +n+ elements, of the enumerable.
# If the enumerable is empty, the first form returns <code>nil</code>, and the
# second form returns an empty array.
#
# @mrbgem mruby-enum-ext
def first(n=NONE)
if n == NONE
self.each do |*val|
......@@ -244,6 +252,8 @@ module Enumerable
# If an argument is given, the number of items in +enum+ that
# are equal to +item+ are counted. If a block is given, it
# counts the number of elements yielding a true value.
#
# @mrbgem mruby-enum-ext
def count(v=NONE, &block)
count = 0
if block
......@@ -276,6 +286,8 @@ module Enumerable
#
# [1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
# [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
#
# @mrbgem mruby-enum-ext
def flat_map(&block)
return to_enum :flat_map unless block
......@@ -303,7 +315,8 @@ module Enumerable
# If no block is given, an enumerator is returned instead.
#
# %w[albatross dog horse].max_by {|x| x.length } #=> "albatross"
#
# @mrbgem mruby-enum-ext
def max_by(&block)
return to_enum :max_by unless block
......@@ -337,7 +350,8 @@ module Enumerable
# If no block is given, an enumerator is returned instead.
#
# %w[albatross dog horse].min_by {|x| x.length } #=> "dog"
#
# @mrbgem mruby-enum-ext
def min_by(&block)
return to_enum :min_by unless block
......@@ -373,7 +387,8 @@ module Enumerable
# a = %w(albatross dog horse)
# a.minmax #=> ["albatross", "horse"]
# a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
#
# @mrbgem mruby-enum-ext
def minmax(&block)
max = nil
min = nil
......@@ -411,7 +426,8 @@ module Enumerable
# If no block is given, an enumerator is returned instead.
#
# %w(albatross dog horse).minmax_by { |x| x.length } #=> ["dog", "albatross"]
#
# @mrbgem mruby-enum-ext
def minmax_by(&block)
return to_enum :minmax_by unless block
......@@ -454,7 +470,8 @@ module Enumerable
# [].none? #=> true
# [nil, false].none? #=> true
# [nil, true].none? #=> false
#
# @mrbgem mruby-enum-ext
def none?(&block)
if block
self.each do |*val|
......@@ -484,7 +501,7 @@ module Enumerable
# [nil, true, 99].one? #=> false
# [nil, true, false].one? #=> true
#
# @mrbgem mruby-enum-ext
def one?(&block)
count = 0
if block
......@@ -515,7 +532,7 @@ module Enumerable
# (1..10).each_with_object([]) { |i, a| a << i*2 }
# #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
#
# @mrbgem mruby-enum-ext
def each_with_object(obj=nil, &block)
raise ArgumentError, "wrong number of arguments (0 for 1)" if obj.nil?
......@@ -542,7 +559,7 @@ module Enumerable
# 2
# 1
#
# @mrbgem mruby-enum-ext
def reverse_each(&block)
return to_enum :reverse_each unless block
......@@ -574,7 +591,7 @@ module Enumerable
# a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
# a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
#
# @mrbgem mruby-enum-ext
def cycle(n=nil, &block)
return to_enum(:cycle, n) if !block && n.nil?
......@@ -623,7 +640,7 @@ module Enumerable
# (1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> 34
# (1..100).find_index(50) #=> 49
#
# @mrbgem mruby-enum-ext
def find_index(val=NONE, &block)
return to_enum(:find_index, val) if !block && val == NONE
......@@ -653,7 +670,7 @@ module Enumerable
# <code>enum#size</code>. If the size of any argument is less than
# <code>enum#size</code>, <code>nil</code> values are supplied.
#
# @mrbgem mruby-enum-ext
def zip(*arg)
ary = []
arg = arg.map{|a|a.to_a}
......@@ -682,7 +699,7 @@ module Enumerable
# %i[hello world].each_with_index.to_h
# # => {:hello => 0, :world => 1}
#
# @mrbgem mruby-enum-ext
def to_h
h = {}
self.each do |*v|
......
# = Enumerable#lazy implementation
#
# Enumerable#lazy returns an instance of Enumerable::Lazy.
# You can use it just like as normal Enumerable object,
# except these methods act as 'lazy':
#
# - map collect
# - select find_all
# - reject
# - grep
# - drop
# - drop_while
# - take_while
# - flat_map collect_concat
# - zip
#
# == Acknowledgements
#
# Based on https://github.com/yhara/enumerable-lazy
# Inspired by https://github.com/antimon2/enumerable_lz
# http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)
module Enumerable
# = Enumerable#lazy implementation
#
# Enumerable#lazy returns an instance of Enumerable::Lazy.
# You can use it just like as normal Enumerable object,
# except these methods act as 'lazy':
#
# - map collect
# - select find_all
# - reject
# - grep
# - drop
# - drop_while
# - take_while
# - flat_map collect_concat
# - zip
#
# @mrbgem mruby-enum-lazy
def lazy
Lazy.new(self)
end
# == Acknowledgements
#
# Based on https://github.com/yhara/enumerable-lazy
# Inspired by https://github.com/antimon2/enumerable_lz
# http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)
#
# @mrbgem mruby-enum-lazy
class Lazy < Enumerator
def initialize(obj, &block)
super(){|yielder|
......
......@@ -84,7 +84,8 @@
# # implementing an internal iterator.
# puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] }
# # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
#
# @mrbgem mruby-enumerator
class Enumerator
include Enumerable
......
......@@ -195,6 +195,10 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, cha
return proc;
}
/**
*
* @mrbgem mruby-eval
*/
static mrb_value
f_eval(mrb_state *mrb, mrb_value self)
{
......@@ -221,6 +225,10 @@ mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
#define CI_ACC_SKIP -1
/**
*
* @mrbgem mruby-eval
*/
static mrb_value
f_instance_eval(mrb_state *mrb, mrb_value self)
{
......
#include <stdlib.h>
#include "mruby.h"
/**
*
* @mrbgem mruby-exit
*/
static mrb_value
f_exit(mrb_state *mrb, mrb_value self)
{
......
......@@ -368,6 +368,13 @@ mrb_mruby_fiber_gem_init(mrb_state* mrb)
{
struct RClass *c;
/**
* Fiber
*
* @mrbgem mruby-fiber
*/
c = mrb_define_class(mrb, "Fiber", mrb->object_class);
MRB_SET_INSTANCE_TT(c, MRB_TT_FIBER);
......@@ -380,6 +387,11 @@ mrb_mruby_fiber_gem_init(mrb_state* mrb)
mrb_define_class_method(mrb, c, "yield", fiber_yield, MRB_ARGS_ANY());
mrb_define_class_method(mrb, c, "current", fiber_current, MRB_ARGS_NONE());
/**
* FiberError
*
* @mrbgem mruby-fiber
*/
mrb_define_class(mrb, "FiberError", mrb->eStandardError_class);
}
......
class Hash
# ISO does not define Hash#each_pair, so each_pair is defined in gem.
#
# @mrbgem mruby-hash-ext
alias each_pair each
##
......@@ -22,7 +24,7 @@ class Hash
# Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
# Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
#
# @mrbgem mruby-hash-ext
def self.[](*object)
length = object.length
if length == 1
......@@ -80,7 +82,7 @@ class Hash
# h1.merge!(h2) { |key, v1, v2| v1 }
# #=> {"a"=>100, "b"=>200, "c"=>300}
#
# @mrbgem mruby-hash-ext
def merge!(other, &block)
raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
if block
......@@ -122,7 +124,7 @@ class Hash
# prog.rb:2:in 'fetch': key not found (KeyError)
# from prog.rb:2
#
# @mrbgem mruby-hash-ext
def fetch(key, none=NONE, &block)
unless self.key?(key)
if block
......@@ -150,7 +152,7 @@ class Hash
# h = { "a" => 100, "b" => 200, "c" => 300 }
# h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
#
# @mrbgem mruby-hash-ext
def delete_if(&block)
return to_enum :delete_if unless block_given?
......@@ -175,7 +177,7 @@ class Hash
# a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
# a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
#
# @mrbgem mruby-hash-ext
def flatten(level=1)
self.to_a.flatten(level)
end
......@@ -190,7 +192,7 @@ class Hash
# h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
# h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
#
# @mrbgem mruby-hash-ext
def invert
h = Hash.new
self.each {|k, v| h[v] = k }
......@@ -207,7 +209,7 @@ class Hash
#
# If no block is given, an enumerator is returned instead.
#
# @mrbgem mruby-hash-ext
def keep_if(&block)
return to_enum :keep_if unless block_given?
......@@ -232,7 +234,7 @@ class Hash
# h.key(300) #=> "c"
# h.key(999) #=> nil
#
# @mrbgem mruby-hash-ext
def key(val)
self.each do |k, v|
return k if v == val
......@@ -247,6 +249,7 @@ class Hash
# Returns +self+. If called on a subclass of Hash, converts
# the receiver to a Hash object.
#
# @mrbgem mruby-hash-ext
def to_h
self
end
......
......@@ -17,6 +17,8 @@
*
* h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
* h.values_at("cow", "cat") #=> ["bovine", "feline"]
*
* @mrbgem mruby-hash-ext
*/
static mrb_value
......
......@@ -11,6 +11,7 @@
* Symbol.
* If called outside of a method, it returns <code>nil</code>.
*
* @mrbgem mruby-kernel-ext
*/
static mrb_value
mrb_f_method(mrb_state *mrb, mrb_value self)
......@@ -45,6 +46,8 @@ mrb_f_method(mrb_state *mrb, mrb_value self)
* Integer("0930", 10) #=> 930
* Integer("111", 2) #=> 7
* Integer(nil) #=> TypeError
*
* @mrbgem mruby-kernel-ext
*/
static mrb_value
mrb_f_integer(mrb_state *mrb, mrb_value self)
......@@ -67,6 +70,8 @@ mrb_f_integer(mrb_state *mrb, mrb_value self)
* Float(123.456) #=> 123.456
* Float("123.456") #=> 123.456
* Float(nil) #=> TypeError
*
* @mrbgem mruby-kernel-ext
*/
static mrb_value
mrb_f_float(mrb_state *mrb, mrb_value self)
......@@ -88,6 +93,8 @@ mrb_f_float(mrb_state *mrb, mrb_value self)
* String(self) #=> "main"
* String(self.class) #=> "Object"
* String(123456) #=> "123456"
*
* @mrbgem mruby-kernel-ext
*/
static mrb_value
mrb_f_string(mrb_state *mrb, mrb_value self)
......@@ -112,6 +119,7 @@ mrb_f_string(mrb_state *mrb, mrb_value self)
*
* Array(1..5) #=> [1, 2, 3, 4, 5]
*
* @mrbgem mruby-kernel-ext
*/
static mrb_value
mrb_f_array(mrb_state *mrb, mrb_value self)
......@@ -142,6 +150,8 @@ mrb_f_array(mrb_state *mrb, mrb_value self)
* Hash(nil) #=> {}
* Hash(key: :value) #=> {:key => :value}
* Hash([1, 2, 3]) #=> TypeError
*
* @mrbgem mruby-kernel-ext
*/
static mrb_value
mrb_f_hash(mrb_state *mrb, mrb_value self)
......
......@@ -721,6 +721,12 @@ void
mrb_mruby_math_gem_init(mrb_state* mrb)
{
struct RClass *mrb_math;
/**
* Math functions
*
* @mrbgem mruby-math
*/
mrb_math = mrb_define_module(mrb, "Math");
mrb_define_class_under(mrb, mrb_math, "DomainError", mrb->eStandardError_class);
......
module Integral
# @mrbgem mruby-numeric-ext
def div(other)
self.divmod(other)[0]
end
end
#include <limits.h>
#include "mruby.h"
/**
* @mrbgem mruby-numeric-ext
*/
static mrb_value
mrb_int_chr(mrb_state *mrb, mrb_value x)
{
......
......@@ -12,6 +12,7 @@ class Object
# .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
# .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"}
#
# @mrbgem mruby-object-ext
def tap
yield self
self
......
......@@ -7,8 +7,9 @@
* nil.to_a -> []
*
* Always returns an empty array.
*
* @mrbgem mruby-object-ext
*/
static mrb_value
nil_to_a(mrb_state *mrb, mrb_value obj)
{
......@@ -20,8 +21,9 @@ nil_to_a(mrb_state *mrb, mrb_value obj)
* nil.to_f -> 0.0
*
* Always returns zero.
*
* @mrbgem mruby-object-ext
*/
static mrb_value
nil_to_f(mrb_state *mrb, mrb_value obj)
{
......@@ -33,8 +35,9 @@ nil_to_f(mrb_state *mrb, mrb_value obj)
* nil.to_i -> 0
*
* Always returns zero.
*
* @mrbgem mruby-object-ext
*/
static mrb_value
nil_to_i(mrb_state *mrb, mrb_value obj)
{
......@@ -57,8 +60,9 @@ nil_to_i(mrb_state *mrb, mrb_value obj)
* end
* k = KlassWithSecret.new
* k.instance_exec(5) {|x| @secret+x } #=> 104
*
* @mrbgem mruby-object-ext
*/
static mrb_value
mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
{
......
......@@ -174,6 +174,12 @@ os_each_object(mrb_state *mrb, mrb_value self)
void
mrb_mruby_objectspace_gem_init(mrb_state *mrb)
{
/**
* ObjectSpace
*
* @mrbgem mruby-objectspace
*/
struct RClass *os = mrb_define_module(mrb, "ObjectSpace");
mrb_define_class_method(mrb, os, "count_objects", os_count_objects, MRB_ARGS_OPT(1));
mrb_define_class_method(mrb, os, "each_object", os_each_object, MRB_ARGS_OPT(1));
......
##
# Kernel
#
# ISO 15.3.1
module Kernel
##
# Invoke method +print+ on STDOUT and passing +*args+
#
# @mrbgem mruby-print
# ISO 15.3.1.2.10
def print(*args)
i = 0
......@@ -19,6 +17,7 @@ module Kernel
##
# Invoke method +puts+ on STDOUT and passing +*args*+
#
# @mrbgem mruby-print
# ISO 15.3.1.2.11
def puts(*args)
i = 0
......@@ -36,6 +35,7 @@ module Kernel
##
# Print human readable object description
#
# @mrbgem mruby-print
# ISO 15.3.1.3.34
def p(*args)
i = 0
......
class Proc
# @mrbgem mruby-proc-ext
def ===(*args)
call(*args)
end
# @mrbgem mruby-proc-ext
def yield(*args)
call(*args)
end
# @mrbgem mruby-proc-ext
def to_proc
self
end
# @mrbgem mruby-proc-ext
def curry(arity=self.arity)
type = :proc
abs = lambda {|a| a < 0 ? -a - 1 : a}
......
......@@ -5,6 +5,9 @@
#include "mruby/string.h"
#include "mruby/debug.h"
/**
* @mrbgem mruby-proc-ext
*/
static mrb_value
mrb_proc_lambda(mrb_state *mrb, mrb_value self)
{
......@@ -12,6 +15,9 @@ mrb_proc_lambda(mrb_state *mrb, mrb_value self)
return mrb_bool_value(MRB_PROC_STRICT_P(p));
}
/**
* @mrbgem mruby-proc-ext
*/
static mrb_value
mrb_proc_source_location(mrb_state *mrb, mrb_value self)
{
......@@ -33,6 +39,9 @@ mrb_proc_source_location(mrb_state *mrb, mrb_value self)
}
}
/**
* @mrbgem mruby-proc-ext
*/
static mrb_value
mrb_proc_inspect(mrb_state *mrb, mrb_value self)
{
......@@ -67,6 +76,9 @@ mrb_proc_inspect(mrb_state *mrb, mrb_value self)
return str;
}
/**
* @mrbgem mruby-proc-ext
*/
static mrb_value
mrb_kernel_proc(mrb_state *mrb, mrb_value self)
{
......@@ -88,8 +100,9 @@ mrb_kernel_proc(mrb_state *mrb, mrb_value self)
*
* prc = lambda{|x, y=42, *other|}
* prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
*
* @mrbgem mruby-proc-ext
*/
static mrb_value
mrb_proc_parameters(mrb_state *mrb, mrb_value self)
{
......
......@@ -104,6 +104,9 @@ get_random_state(mrb_state *mrb)
return DATA_GET_PTR(mrb, random_val, &mt_state_type, mt_state);
}
/**
* @mrbgem mruby-random
*/
static mrb_value
mrb_random_g_rand(mrb_state *mrb, mrb_value self)
{
......@@ -111,6 +114,9 @@ mrb_random_g_rand(mrb_state *mrb, mrb_value self)
return mrb_random_rand(mrb, random);
}
/**
* @mrbgem mruby-random
*/
static mrb_value
mrb_random_g_srand(mrb_state *mrb, mrb_value self)
{
......@@ -196,8 +202,9 @@ mrb_random_srand(mrb_state *mrb, mrb_value self)
* ary.shuffle! -> ary
*
* Shuffles elements in self in place.
*
* @mrbgem mruby-random
*/
static mrb_value
mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary)
{
......@@ -234,8 +241,9 @@ mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary)
* ary.shuffle -> new_ary
*
* Returns a new array with elements of self shuffled.
*
* @mrbgem mruby-random
*/
static mrb_value
mrb_ary_shuffle(mrb_state *mrb, mrb_value ary)
{
......@@ -258,8 +266,9 @@ mrb_ary_shuffle(mrb_state *mrb, mrb_value ary)
*
* If the array is empty the first form returns +nil+ and the second form
* returns an empty array.
*
* @mrbgem mruby-random
*/
static mrb_value
mrb_ary_sample(mrb_state *mrb, mrb_value ary)
{
......@@ -323,6 +332,9 @@ void mrb_mruby_random_gem_init(mrb_state *mrb)
mrb_define_method(mrb, mrb->kernel_module, "rand", mrb_random_g_rand, MRB_ARGS_OPT(1));
mrb_define_method(mrb, mrb->kernel_module, "srand", mrb_random_g_srand, MRB_ARGS_OPT(1));
/**
* @mrbgem mruby-random
*/
random = mrb_define_class(mrb, "Random", mrb->object_class);
MRB_SET_INSTANCE_TT(random, MRB_TT_DATA);
mrb_define_class_method(mrb, random, "rand", mrb_random_g_rand, MRB_ARGS_OPT(1));
......
......@@ -38,6 +38,8 @@ r_lt(mrb_state *mrb, mrb_value a, mrb_value b)
* ("a".."z").cover?("c") #=> true
* ("a".."z").cover?("5") #=> false
* ("a".."z").cover?("cc") #=> true
*
* @mrbgem mruby-range-ext
*/
static mrb_value
mrb_range_cover(mrb_state *mrb, mrb_value range)
......@@ -75,6 +77,8 @@ mrb_range_cover(mrb_state *mrb, mrb_value range)
*
* (10..20).first #=> 10
* (10..20).first(3) #=> [10, 11, 12]
*
* @mrbgem mruby-range-ext
*/
static mrb_value
mrb_range_first(mrb_state *mrb, mrb_value range)
......@@ -106,6 +110,8 @@ mrb_range_first(mrb_state *mrb, mrb_value range)
* (10...20).last #=> 20
* (10..20).last(3) #=> [18, 19, 20]
* (10...20).last(3) #=> [17, 18, 19]
*
* @mrbgem mruby-range-ext
*/
static mrb_value
mrb_range_last(mrb_state *mrb, mrb_value range)
......@@ -131,8 +137,9 @@ mrb_range_last(mrb_state *mrb, mrb_value range)
*
* (10..20).size #=> 11
* ('a'..'z').size #=> nil
*
* @mrbgem mruby-range-ext
*/
static mrb_value
mrb_range_size(mrb_state *mrb, mrb_value range)
{
......
class String
# @mrbgem mruby-sprintf
def %(args)
if args.is_a? Array
sprintf(self, *args)
......
......@@ -6,6 +6,9 @@
#include "mruby.h"
/**
* @mrbgem mruby-sprintf
*/
mrb_value mrb_f_sprintf(mrb_state *mrb, mrb_value obj); /* in sprintf.c */
void
......
......@@ -9,6 +9,7 @@ class String
# a = "abcde"
# a.clear #=> ""
#
# @mrbgem mruby-string-ext
def clear
self.replace("")
end
......@@ -23,6 +24,7 @@ class String
# " hello ".lstrip #=> "hello "
# "hello".lstrip #=> "hello"
#
# @mrbgem mruby-string-ext
def lstrip
a = 0
z = self.size - 1
......@@ -40,6 +42,7 @@ class String
# " hello ".rstrip #=> " hello"
# "hello".rstrip #=> "hello"
#
# @mrbgem mruby-string-ext
def rstrip
a = 0
z = self.size - 1
......@@ -56,6 +59,7 @@ class String
# " hello ".strip #=> "hello"
# "\tgoodbye\r\n".strip #=> "goodbye"
#
# @mrbgem mruby-string-ext
def strip
a = 0
z = self.size - 1
......@@ -75,6 +79,7 @@ class String
# " hello ".lstrip #=> "hello "
# "hello".lstrip! #=> nil
#
# @mrbgem mruby-string-ext
def lstrip!
s = self.lstrip
(s == self) ? nil : self.replace(s)
......@@ -91,6 +96,7 @@ class String
# " hello ".rstrip #=> " hello"
# "hello".rstrip! #=> nil
#
# @mrbgem mruby-string-ext
def rstrip!
s = self.rstrip
(s == self) ? nil : self.replace(s)
......@@ -103,6 +109,7 @@ class String
# Removes leading and trailing whitespace from <i>str</i>. Returns
# <code>nil</code> if <i>str</i> was not altered.
#
# @mrbgem mruby-string-ext
def strip!
s = self.strip
(s == self) ? nil : self.replace(s)
......@@ -119,6 +126,7 @@ class String
# "abcdef".casecmp("abcdefg") #=> -1
# "abcdef".casecmp("ABCDEF") #=> 0
#
# @mrbgem mruby-string-ext
def casecmp(str)
self.downcase <=> str.to_str.downcase
rescue NoMethodError
......@@ -136,6 +144,7 @@ class String
end
end
# @mrbgem mruby-string-ext
def rpartition(sep)
raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
n = rindex(sep)
......@@ -163,6 +172,7 @@ class String
# string.slice!("r") #=> "r"
# string #=> "thsa sting"
#
# @mrbgem mruby-string-ext
def slice!(arg1, arg2=nil)
raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
......@@ -234,6 +244,7 @@ class String
# "abcd".insert(-3, 'X') #=> "abXcd"
# "abcd".insert(-1, 'X') #=> "abcdX"
#
# @mrbgem mruby-string-ext
def insert(idx, str)
pos = idx.to_i
pos += self.size + 1 if pos < 0
......@@ -256,6 +267,8 @@ class String
# "hello".ljust(4) #=> "hello"
# "hello".ljust(20) #=> "hello "
# "hello".ljust(20, '1234') #=> "hello123412341234123"
#
# @mrbgem mruby-string-ext
def ljust(idx, padstr = ' ')
if idx <= self.size
return self
......@@ -297,6 +310,7 @@ class String
# "25".upto("5").to_a #=> []
# "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
#
# @mrbgem mruby-string-ext
def upto(other_str, excl=false, &block)
return to_enum :upto, other_str, excl unless block
......@@ -311,6 +325,7 @@ class String
end
end
# @mrbgem mruby-string-ext
def chars(&block)
if block_given?
self.split('').map do |i|
......@@ -323,6 +338,7 @@ class String
end
alias each_char chars
# @mrbgem mruby-string-ext
def codepoints(&block)
len = self.size
......
......@@ -5,6 +5,9 @@
#include "mruby/string.h"
#include "mruby/range.h"
/**
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_getbyte(mrb_state *mrb, mrb_value str)
{
......@@ -19,6 +22,9 @@ mrb_str_getbyte(mrb_state *mrb, mrb_value str)
return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[pos]);
}
/**
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_setbyte(mrb_state *mrb, mrb_value str)
{
......@@ -38,6 +44,9 @@ mrb_str_setbyte(mrb_state *mrb, mrb_value str)
return mrb_fixnum_value((unsigned char)byte);
}
/**
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_byteslice(mrb_state *mrb, mrb_value str)
{
......@@ -79,6 +88,8 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str)
* Equivalent to <code>String#swapcase</code>, but modifies the receiver in
* place, returning <i>str</i>, or <code>nil</code> if no changes were made.
* Note: case conversion is effective only in ASCII region.
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
......@@ -116,6 +127,8 @@ mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
*
* "Hello".swapcase #=> "hELLO"
* "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_swapcase(mrb_state *mrb, mrb_value self)
......@@ -141,6 +154,8 @@ mrb_str_swapcase(mrb_state *mrb, mrb_value self)
* a = "hello "
* a << "world" #=> "hello world"
* a.concat(33) #=> "hello world!"
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_concat2(mrb_state *mrb, mrb_value self)
......@@ -163,6 +178,8 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self)
* "hello".start_with?("heaven", "hell") #=> true
* "hello".start_with?("heaven", "paradise") #=> false
* "h".start_with?("heaven", "hell") #=> false
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
......@@ -192,6 +209,8 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self)
* str.end_with?([suffixes]+) -> true or false
*
* Returns true if +str+ ends with one of the +suffixes+ given.
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_end_with(mrb_state *mrb, mrb_value self)
......@@ -218,12 +237,18 @@ mrb_str_end_with(mrb_state *mrb, mrb_value self)
return mrb_false_value();
}
/*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_hex(mrb_state *mrb, mrb_value self)
{
return mrb_str_to_inum(mrb, self, 16, FALSE);
}
/*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_oct(mrb_state *mrb, mrb_value self)
{
......@@ -238,6 +263,8 @@ mrb_str_oct(mrb_state *mrb, mrb_value self)
*
* a = "abcde"
* a.chr #=> "a"
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_chr(mrb_state *mrb, mrb_value self)
......@@ -245,6 +272,9 @@ mrb_str_chr(mrb_state *mrb, mrb_value self)
return mrb_str_substr(mrb, self, 0, 1);
}
/*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_fixnum_chr(mrb_state *mrb, mrb_value num)
{
......@@ -298,6 +328,8 @@ mrb_fixnum_chr(mrb_state *mrb, mrb_value num)
*
* a = "abc\ndef"
* a.lines #=> ["abc\n", "def"]
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_lines(mrb_state *mrb, mrb_value self)
......@@ -345,6 +377,8 @@ mrb_str_lines(mrb_state *mrb, mrb_value self)
*
* a = "abc"
* a.succ #=> "abd"
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
......@@ -419,6 +453,9 @@ mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
return self;
}
/*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_succ(mrb_state *mrb, mrb_value self)
{
......@@ -438,6 +475,8 @@ mrb_str_succ(mrb_state *mrb, mrb_value self)
* a = "world"
* a.prepend("hello ") #=> "hello world"
* a #=> "hello world"
*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_prepend(mrb_state *mrb, mrb_value self)
......@@ -516,6 +555,9 @@ utf8code(unsigned char* p)
return p[0];
}
/*
* @mrbgem mruby-string-ext
*/
static mrb_value
mrb_str_ord(mrb_state* mrb, mrb_value str)
{
......
......@@ -739,26 +739,28 @@ mrb_struct_values_at(mrb_state *mrb, mrb_value self)
return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
}
/*
* A <code>Struct</code> is a convenient way to bundle a number of
* attributes together, using accessor methods, without having to write
* an explicit class.
*
* The <code>Struct</code> class is a generator of specific classes,
* each one of which is defined to hold a set of variables and their
* accessors. In these examples, we'll call the generated class
* "<i>Customer</i>Class," and we'll show an example instance of that
* class as "<i>Customer</i>Inst."
*
* In the descriptions that follow, the parameter <i>symbol</i> refers
* to a symbol, which is either a quoted string or a
* <code>Symbol</code> (such as <code>:name</code>).
*/
void
mrb_mruby_struct_gem_init(mrb_state* mrb)
{
struct RClass *st;
/*
* A <code>Struct</code> is a convenient way to bundle a number of
* attributes together, using accessor methods, without having to write
* an explicit class.
*
* The <code>Struct</code> class is a generator of specific classes,
* each one of which is defined to hold a set of variables and their
* accessors. In these examples, we'll call the generated class
* "<i>Customer</i>Class," and we'll show an example instance of that
* class as "<i>Customer</i>Inst."
*
* In the descriptions that follow, the parameter <i>symbol</i> refers
* to a symbol, which is either a quoted string or a
* <code>Symbol</code> (such as <code>:name</code>).
*
* @mrbgem mruby-struct
*/
st = mrb_define_class(mrb, "Struct", mrb->object_class);
mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, MRB_ARGS_ANY()); /* 15.2.18.3.1 */
......
class Symbol
include Comparable
# @mrbgem mruby-symbol-ext
alias intern to_sym
def to_proc
......@@ -14,7 +15,8 @@ class Symbol
# sym.capitalize -> symbol
#
# Same as <code>sym.to_s.capitalize.intern</code>.
#
# @mrbgem mruby-symbol-ext
def capitalize
(self.to_s.capitalize! || self).to_sym
end
......@@ -24,7 +26,8 @@ class Symbol
# sym.downcase -> symbol
#
# Same as <code>sym.to_s.downcase.intern</code>.
#
# @mrbgem mruby-symbol-ext
def downcase
(self.to_s.downcase! || self).to_sym
end
......@@ -34,7 +37,8 @@ class Symbol
# sym.upcase -> symbol
#
# Same as <code>sym.to_s.upcase.intern</code>.
#
# @mrbgem mruby-symbol-ext
def upcase
(self.to_s.upcase! || self).to_sym
end
......@@ -44,7 +48,8 @@ class Symbol
# sym.casecmp(other) -> -1, 0, +1 or nil
#
# Case-insensitive version of <code>Symbol#<=></code>.
#
# @mrbgem mruby-symbol-ext
def casecmp(other)
return nil unless other.kind_of?(Symbol)
lhs = self.to_s; lhs.upcase!
......@@ -57,7 +62,8 @@ class Symbol
# sym.empty? -> true or false
#
# Returns that _sym_ is :"" or not.
#
# @mrbgem mruby-symbol-ext
def empty?
self.length == 0
end
......
......@@ -21,6 +21,8 @@ typedef struct symbol_name {
* :default_proc, :compact, :extend,
* :Tms, :getwd, :$=, :ThreadGroup,
* :wait2, :$>]
*
* @mrbgem mruby-symbol-ext
*/
static mrb_value
mrb_sym_all_symbols(mrb_state *mrb, mrb_value self)
......@@ -40,6 +42,8 @@ mrb_sym_all_symbols(mrb_state *mrb, mrb_value self)
* sym.length -> integer
*
* Same as <code>sym.to_s.length</code>.
*
* @mrbgem mruby-symbol-ext
*/
static mrb_value
mrb_sym_length(mrb_state *mrb, mrb_value self)
......
# Time class
#
# @mrbgem mruby-time
class Time
def sunday?; wday == 0 end
def monday?; wday == 1 end
......
# @mrbgem mruby-toplevel-ext
def self.include (*modules)
self.class.include(*modules)
end
# @mrbgem mruby-toplevel-ext
def self.private(*methods)
end
# @mrbgem mruby-toplevel-ext
def self.protected(*methods)
end
# @mrbgem mruby-toplevel-ext
def self.public(*methods)
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment