summaryrefslogtreecommitdiffhomepage
path: root/mrb_gems/keyword_arguments_example
diff options
context:
space:
mode:
Diffstat (limited to 'mrb_gems/keyword_arguments_example')
-rw-r--r--mrb_gems/keyword_arguments_example/LICENSE22
-rw-r--r--mrb_gems/keyword_arguments_example/README.md2
-rw-r--r--mrb_gems/keyword_arguments_example/mrbgem.rake4
-rw-r--r--mrb_gems/keyword_arguments_example/src/keyword_arguments_example.c66
4 files changed, 94 insertions, 0 deletions
diff --git a/mrb_gems/keyword_arguments_example/LICENSE b/mrb_gems/keyword_arguments_example/LICENSE
new file mode 100644
index 0000000..a632bdf
--- /dev/null
+++ b/mrb_gems/keyword_arguments_example/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2023 Tradam
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
diff --git a/mrb_gems/keyword_arguments_example/README.md b/mrb_gems/keyword_arguments_example/README.md
new file mode 100644
index 0000000..71eed2d
--- /dev/null
+++ b/mrb_gems/keyword_arguments_example/README.md
@@ -0,0 +1,2 @@
+# KeywordArgumentsExample
+An mruby gem created by Tradam using mruby_gem_scaffolding.
diff --git a/mrb_gems/keyword_arguments_example/mrbgem.rake b/mrb_gems/keyword_arguments_example/mrbgem.rake
new file mode 100644
index 0000000..6a1fc1e
--- /dev/null
+++ b/mrb_gems/keyword_arguments_example/mrbgem.rake
@@ -0,0 +1,4 @@
+MRuby::Gem::Specification.new('keyword_arguments_example') do |spec|
+ spec.license = 'MIT'
+ spec.author = 'Tradam'
+end
diff --git a/mrb_gems/keyword_arguments_example/src/keyword_arguments_example.c b/mrb_gems/keyword_arguments_example/src/keyword_arguments_example.c
new file mode 100644
index 0000000..c90b507
--- /dev/null
+++ b/mrb_gems/keyword_arguments_example/src/keyword_arguments_example.c
@@ -0,0 +1,66 @@
+#include <mruby.h>
+#include <stdio.h>
+
+// defining the function to be later bound to a ruby method
+static mrb_value
+multiply_numbers(mrb_state *mrb, mrb_value self)
+{
+ // the number of keyword arguments we expect
+ uint32_t kw_num = 2;
+
+ const mrb_sym kw_names[] = { // the array where we specify what we expect the keyword arguments to be named
+ mrb_intern_lit(mrb, "x"), // we will expect an 'x' keyword
+ mrb_intern_lit(mrb, "y"), // we will expect a 'y' keyword
+ };
+
+ // initialize the array where the values of they kwargs will be temporarily stored
+ mrb_value kw_values[kw_num];
+
+ // When getting arguments, the function expects all the data we
+ // created above to be formatted into a specific struct. So we
+ // create this struct here.
+ const mrb_kwargs kwargs = {
+ kw_num, // number of kwargs
+ 0, // number of required kwargs
+ kw_names, // names of kwargs
+ kw_values, // values of kwargs(will be filled out)
+ NULL // remaining kwargs(NULL specifies to error if other kwargs or args are given)
+ };
+
+ mrb_get_args(mrb, "|:", &kwargs); // get the arguments
+
+ if (mrb_undef_p(kw_values[0])){ // check if we actually recieved the first kwarg
+ kw_values[0] = mrb_fixnum_value(5); // set a default value if we didnt
+ // notice that because kw_values only holds ruby objects
+ // we must convert the number "5" into a ruby object by
+ // using the 'mrb_fixnum_value' function
+ }
+
+ if (mrb_undef_p(kw_values[1])){ // ditto with second kwarg
+ kw_values[1] = mrb_fixnum_value(5);
+ }
+
+ // we convert the ruby objects back into C
+ // ints, mutiply them and then return the
+ // value as a ruby object
+ return mrb_fixnum_value(mrb_as_int(mrb,kw_values[0]) * mrb_as_int(mrb,kw_values[1]));
+}
+
+// gem initializer
+void
+mrb_keyword_arguments_example_gem_init(mrb_state* mrb) {
+ struct RClass *keyword_arguments_example_class = mrb_define_module(mrb, "KeywordArgumentsExample");
+ mrb_define_class_method(
+ mrb, // Mruby VM state
+ keyword_arguments_example_class, // Class we bind method to
+ "multiply_numbers", // Name of method
+ multiply_numbers, // Function we are binding as a method
+ MRB_ARGS_ANY() // How many arguments are optional/required
+ );
+}
+
+// gem finalizer
+void
+mrb_keyword_arguments_example_gem_final(mrb_state* mrb) {
+
+}