become 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +10 -0
- data/ext/become.c +41 -0
- data/ext/extconf.rb +2 -0
- data/test/test_become.rb +37 -0
- metadata +70 -0
data/README.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
`Object#become` will swap two Ruby objects, similar to Smalltalk's become.
|
2
|
+
|
3
|
+
Example usage:
|
4
|
+
|
5
|
+
cat = Cat.new
|
6
|
+
dog = Dog.new
|
7
|
+
cat.become(dog) # cat is now a Dog and dog is now a Cat
|
8
|
+
|
9
|
+
License: MIT License (http://www.opensource.org/licenses/mit-license.html)
|
10
|
+
|
data/ext/become.c
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
|
3
|
+
static void become_impl(VALUE obj1, VALUE obj2) {
|
4
|
+
struct RObject * robj1 = ROBJECT(obj1);
|
5
|
+
struct RObject * robj2 = ROBJECT(obj2);
|
6
|
+
struct RObject tmp_obj;
|
7
|
+
|
8
|
+
tmp_obj = *robj1;
|
9
|
+
*robj1 = *robj2;
|
10
|
+
*robj2 = tmp_obj;
|
11
|
+
}
|
12
|
+
|
13
|
+
/* call-seq:
|
14
|
+
* obj1.become(obj2)
|
15
|
+
*
|
16
|
+
* Swaps obj1 and obj2.
|
17
|
+
*/
|
18
|
+
static VALUE ruby_become(VALUE self, VALUE newobj) {
|
19
|
+
rb_secure(1);
|
20
|
+
|
21
|
+
if(TYPE(self) != T_OBJECT || TYPE(newobj) != T_OBJECT) {
|
22
|
+
rb_raise(
|
23
|
+
rb_eTypeError,
|
24
|
+
"%s cannot become %s",
|
25
|
+
STR2CSTR(rb_mod_name(rb_obj_class(self))),
|
26
|
+
STR2CSTR(rb_mod_name(rb_obj_class(newobj))));
|
27
|
+
}
|
28
|
+
|
29
|
+
if(OBJ_FROZEN(self) || OBJ_FROZEN(newobj)) {
|
30
|
+
rb_error_frozen("object");
|
31
|
+
}
|
32
|
+
|
33
|
+
become_impl(self, newobj);
|
34
|
+
|
35
|
+
return self;
|
36
|
+
}
|
37
|
+
|
38
|
+
void Init_become(void) {
|
39
|
+
rb_define_method(rb_cObject, "become", ruby_become, 1);
|
40
|
+
}
|
41
|
+
|
data/ext/extconf.rb
ADDED
data/test/test_become.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'become'
|
3
|
+
|
4
|
+
class BecomeTest < Test::Unit::TestCase
|
5
|
+
class Foo
|
6
|
+
def initialize
|
7
|
+
@a = 1
|
8
|
+
@b = 2
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class BarBase
|
13
|
+
end
|
14
|
+
|
15
|
+
class Bar < BarBase
|
16
|
+
def initialize
|
17
|
+
@c = 3
|
18
|
+
@d = 4
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_become
|
23
|
+
foo = Foo.new
|
24
|
+
bar = Bar.new
|
25
|
+
|
26
|
+
foo.become(bar)
|
27
|
+
|
28
|
+
assert_equal Bar, foo.class
|
29
|
+
assert_equal 3, foo.instance_eval { @c }
|
30
|
+
assert_equal 4, foo.instance_eval { @d }
|
31
|
+
|
32
|
+
assert_equal Foo, bar.class
|
33
|
+
assert_equal 1, bar.instance_eval { @a }
|
34
|
+
assert_equal 2, bar.instance_eval { @b }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: become
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Paul Brannan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-06-11 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: |
|
22
|
+
Object#become will swap two Ruby objects, similar to Smalltalk's become.
|
23
|
+
|
24
|
+
email: curlypaul924@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions:
|
28
|
+
- ext/extconf.rb
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README.md
|
31
|
+
files:
|
32
|
+
- ext/extconf.rb
|
33
|
+
- ext/become.c
|
34
|
+
- test/test_become.rb
|
35
|
+
- README.md
|
36
|
+
homepage: http://github.com/cout/become/
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Become
|
69
|
+
test_files:
|
70
|
+
- test/test_become.rb
|