summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrbgems/mruby-complex/mrblib/complex.rb37
-rw-r--r--mrbgems/mruby-complex/src/complex.c10
-rw-r--r--mrbgems/mruby-metaprog/src/metaprog.c26
-rw-r--r--src/string.c3
-rw-r--r--src/vm.c25
5 files changed, 40 insertions, 61 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb
index 4c0c19c70..0299e7675 100644
--- a/mrbgems/mruby-complex/mrblib/complex.rb
+++ b/mrbgems/mruby-complex/mrblib/complex.rb
@@ -1,19 +1,8 @@
class Complex < Numeric
- def initialize(real, imaginary)
- real = real.to_f unless real.is_a? Numeric
- imaginary = imaginary.to_f unless imaginary.is_a? Numeric
- @real = real
- @imaginary = imaginary
- end
-
def self.polar(abs, arg = 0)
Complex(abs * Math.cos(arg), abs * Math.sin(arg))
end
- def self.rectangular(real, imaginary = 0)
- _new(real, imaginary)
- end
-
def inspect
"(#{to_s})"
end
@@ -23,43 +12,43 @@ class Complex < Numeric
end
def +@
- Complex._new(real, imaginary)
+ Complex(real, imaginary)
end
def -@
- Complex._new(-real, -imaginary)
+ Complex(-real, -imaginary)
end
def +(rhs)
if rhs.is_a? Complex
- Complex._new(real + rhs.real, imaginary + rhs.imaginary)
+ Complex(real + rhs.real, imaginary + rhs.imaginary)
elsif rhs.is_a? Numeric
- Complex._new(real + rhs, imaginary)
+ Complex(real + rhs, imaginary)
end
end
def -(rhs)
if rhs.is_a? Complex
- Complex._new(real - rhs.real, imaginary - rhs.imaginary)
+ Complex(real - rhs.real, imaginary - rhs.imaginary)
elsif rhs.is_a? Numeric
- Complex._new(real - rhs, imaginary)
+ Complex(real - rhs, imaginary)
end
end
def *(rhs)
if rhs.is_a? Complex
- Complex._new(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary)
+ Complex(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary)
elsif rhs.is_a? Numeric
- Complex._new(real * rhs, imaginary * rhs)
+ Complex(real * rhs, imaginary * rhs)
end
end
def /(rhs)
if rhs.is_a? Complex
div = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary
- Complex._new((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div)
+ Complex((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div)
elsif rhs.is_a? Numeric
- Complex._new(real / rhs, imaginary / rhs)
+ Complex(real / rhs, imaginary / rhs)
end
end
alias_method :quo, :/
@@ -117,12 +106,6 @@ class Complex < Numeric
alias_method :imag, :imaginary
end
-module Kernel
- def Complex(real, imaginary = 0)
- Complex.rectangular(real, imaginary)
- end
-end
-
[Fixnum, Float].each do |cls|
[:+, :-, :*, :/, :==].each do |op|
cls.instance_exec do
diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c
index 1b030c317..5371332cd 100644
--- a/mrbgems/mruby-complex/src/complex.c
+++ b/mrbgems/mruby-complex/src/complex.c
@@ -76,11 +76,11 @@ complex_imaginary(mrb_state *mrb, mrb_value self)
}
static mrb_value
-complex_s_new(mrb_state *mrb, mrb_value self)
+complex_s_rect(mrb_state *mrb, mrb_value self)
{
- mrb_float real, imaginary;
+ mrb_float real, imaginary = 0.0;
- mrb_get_args(mrb, "ff", &real, &imaginary);
+ mrb_get_args(mrb, "f|f", &real, &imaginary);
return complex_new(mrb, real, imaginary);
}
@@ -125,7 +125,9 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb)
comp = mrb_define_class(mrb, "Complex", mrb_class_get(mrb, "Numeric"));
//MRB_SET_INSTANCE_TT(comp, MRB_TT_ISTRUCT);
mrb_undef_class_method(mrb, comp, "new");
- mrb_define_class_method(mrb, comp, "_new", complex_s_new, MRB_ARGS_REQ(2));
+ mrb_define_class_method(mrb, comp, "rectangular", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
+ mrb_define_class_method(mrb, comp, "rect", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
+ mrb_define_method(mrb, mrb->kernel_module, "Complex", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE());
#ifndef MRB_WITHOUT_FLOAT
diff --git a/mrbgems/mruby-metaprog/src/metaprog.c b/mrbgems/mruby-metaprog/src/metaprog.c
index 0aafb4c34..38f75d460 100644
--- a/mrbgems/mruby-metaprog/src/metaprog.c
+++ b/mrbgems/mruby-metaprog/src/metaprog.c
@@ -657,8 +657,30 @@ mrb_mod_s_constants(mrb_state *mrb, mrb_value mod)
return mrb_nil_value(); /* not reached */
}
-/* implementation of Module.nesting */
-mrb_value mrb_mod_s_nesting(mrb_state*, mrb_value);
+mrb_value
+mrb_mod_s_nesting(mrb_state *mrb, mrb_value mod)
+{
+ struct RProc *proc;
+ mrb_value ary;
+ struct RClass *c = NULL;
+
+ mrb_get_args(mrb, "");
+ ary = mrb_ary_new(mrb);
+ proc = mrb->c->ci[-1].proc; /* callee proc */
+ mrb_assert(!MRB_PROC_CFUNC_P(proc));
+ while (proc) {
+ if (MRB_PROC_SCOPE_P(proc)) {
+ struct RClass *c2 = MRB_PROC_TARGET_CLASS(proc);
+
+ if (c2 != c) {
+ c = c2;
+ mrb_ary_push(mrb, ary, mrb_obj_value(c));
+ }
+ }
+ proc = proc->upper;
+ }
+ return ary;
+}
void
mrb_mruby_metaprog_gem_init(mrb_state* mrb)
diff --git a/src/string.c b/src/string.c
index db2d73e32..bfe73b359 100644
--- a/src/string.c
+++ b/src/string.c
@@ -449,9 +449,6 @@ str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
if (clen == 0) {
len = 0;
}
- else if (beg < 0) {
- beg = clen + beg;
- }
if (beg > clen) return mrb_nil_value();
if (beg < 0) {
beg += clen;
diff --git a/src/vm.c b/src/vm.c
index 8dc6623d1..0a6d4af8d 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -830,31 +830,6 @@ mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const
return mrb_exec_irep(mrb, self, p);
}
-mrb_value
-mrb_mod_s_nesting(mrb_state *mrb, mrb_value mod)
-{
- struct RProc *proc;
- mrb_value ary;
- struct RClass *c = NULL;
-
- mrb_get_args(mrb, "");
- ary = mrb_ary_new(mrb);
- proc = mrb->c->ci[-1].proc; /* callee proc */
- mrb_assert(!MRB_PROC_CFUNC_P(proc));
- while (proc) {
- if (MRB_PROC_SCOPE_P(proc)) {
- struct RClass *c2 = MRB_PROC_TARGET_CLASS(proc);
-
- if (c2 != c) {
- c = c2;
- mrb_ary_push(mrb, ary, mrb_obj_value(c));
- }
- }
- proc = proc->upper;
- }
- return ary;
-}
-
static struct RBreak*
break_new(mrb_state *mrb, struct RProc *p, mrb_value val)
{