Permalink
Browse files

add "has_key?" and aliases.

  • Loading branch information...
tsahara committed Jan 10, 2014
1 parent 24f9851 commit 87e73c4c94baa51eb3c3d325fc847ce581565006
Showing with 28 additions and 1 deletion.
  1. +2 −1 README.md
  2. +5 −0 mrblib/env.rb
  3. +15 −0 src/env.c
  4. +6 −0 test/env_test.rb
View
@@ -7,7 +7,8 @@ for environment variables.
### Methods
```
[] []= clear delete inspect keys size store to_a to_hash to_s values
[] []= clear delete has_key? include? inspect key? keys
member? size store to_a to_hash to_s values
```
View
@@ -0,0 +1,5 @@
class << ENV
alias include? has_key?
alias key? has_key?
alias member? has_key?
end
View
@@ -120,6 +120,20 @@ mrb_env_aget(mrb_state *mrb, mrb_value self)
return mrb_env_getenv(mrb, key);
}
mrb_value
mrb_env_has_key(mrb_state *mrb, mrb_value self)
{
mrb_value name;
const char *key;
mrb_get_args(mrb, "S", &name);
key = mrb_str_to_cstr(mrb, name);
if (getenv(key) != NULL) {
return mrb_true_value();
} else {
return mrb_false_value();
}
}
mrb_value
mrb_env_keys(mrb_state *mrb, mrb_value self)
{
@@ -282,6 +296,7 @@ mrb_mruby_env_gem_init(mrb_state *mrb)
mrb_define_singleton_method(mrb, e,"[]=", mrb_env_aset, MRB_ARGS_REQ(2));
mrb_define_singleton_method(mrb, e,"clear", mrb_env_clear, MRB_ARGS_NONE());
mrb_define_singleton_method(mrb, e,"delete", mrb_env_delete, MRB_ARGS_REQ(1));
mrb_define_singleton_method(mrb, e,"has_key?", mrb_env_has_key, MRB_ARGS_REQ(1));
mrb_define_singleton_method(mrb, e,"inspect", mrb_env_inspect, MRB_ARGS_NONE());
mrb_define_singleton_method(mrb, e,"keys", mrb_env_keys, MRB_ARGS_NONE());
mrb_define_singleton_method(mrb, e,"size", mrb_env_size, MRB_ARGS_NONE());
View
@@ -68,6 +68,12 @@ def test_env_to_s
assert_equal("ENV", ENV.to_s)
end
def test_env_has_key
set_dummy_env
assert_true ENV.has_key?("FOO")
assert_false ENV.has_key?("BAR")
end
def test_env_inspect
set_dummy_env
assert_equal("{\"FOO\"=>\"bar\"}", ENV.inspect)

0 comments on commit 87e73c4

Please sign in to comment.