summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2023-02-09 15:11:04 -0500
committerrealtradam <[email protected]>2023-02-09 15:11:04 -0500
commit90950ee613c32c248108406a176598d6216e3140 (patch)
tree409575b91cf6a9c7cfa5e242d67db5f72a67a537
parent0767bc19f3d63201c94553e9ee271c56bea946cc (diff)
downloadmruby-playground-90950ee613c32c248108406a176598d6216e3140.tar.gz
mruby-playground-90950ee613c32c248108406a176598d6216e3140.zip
added keyword arguments example
-rw-r--r--build_config.rb1
-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
-rw-r--r--test_suite.rb7
6 files changed, 101 insertions, 1 deletions
diff --git a/build_config.rb b/build_config.rb
index a7c77ff..773f579 100644
--- a/build_config.rb
+++ b/build_config.rb
@@ -5,6 +5,7 @@ MRuby::Build.new do |conf|
# Use mrbgems
conf.gem './mrb_gems/basic_example'
conf.gem './mrb_gems/arguments_and_return_example'
+ conf.gem './mrb_gems/keyword_arguments_example'
# conf.gem 'examples/mrbgems/ruby_extension_example'
# conf.gem 'examples/mrbgems/c_extension_example' do |g|
# g.cc.flags << '-g' # append cflags in this gem
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) {
+
+}
diff --git a/test_suite.rb b/test_suite.rb
index 7ddc1eb..5ceb936 100644
--- a/test_suite.rb
+++ b/test_suite.rb
@@ -6,9 +6,14 @@ BasicExample.say_hello
BasicExample.say_goodbye
puts
-puts " - Multiply Numbers Return Example - "
+puts " - Arguments And Return Example - "
print "3 * 10 = "
puts ArgumentsAndReturnExample.multiply_numbers(3, 10)
puts
+puts " - Keyword Arguments Example - "
+print "(x: 4) * (y: 6) = "
+puts KeywordArgumentsExample.multiply_numbers(x: 4, y: 6)
+puts
+
puts "Reached end of test suite."