Permalink
Browse files

add Object#to_hash

  • Loading branch information...
1 parent 6068342 commit 158ca99916af15a46a91f383bc17fbbbb99b5582 @bakkdoor committed Jan 25, 2013
Showing with 21 additions and 1 deletion.
  1. +13 −0 lib/object.fy
  2. +8 −1 tests/object.fy
View
13 lib/object.fy
@@ -139,6 +139,19 @@ class Object {
[self]
}
+ @@__to_hash_exclude_slots__ = ['_fancy_documentation]
+ def to_hash {
+ """
+ @return @Hash@ representation of @self based on slot values.
+ """
+
+ h = <[]>
+ slots - @@__to_hash_exclude_slots__ each: |s| {
+ h[s]: $ get_slot: s
+ }
+ h
+ }
+
def to_i {
"""
@return @Fixnum@ representation of @self.
View
9 tests/object.fy
@@ -56,7 +56,14 @@ FancySpec describe: Object with: {
<['foo => "bar", 'bar => "baz"]> to_a is =? [['bar, "baz"], ['foo, "bar"]]
}
- it: "returns a correct fixnum representation" when: {
+ it: "returns a hash based on own slot values" with: 'to_hash when: {
+ nil to_hash is: <[]>
+ false to_hash is: <[]>
+ true to_hash is: <[]>
+ (0..10) each: @{ to_hash is: <[]> }
+ }
+
+ it: "returns a correct fixnum representation" with: 'to_i when: {
nil to_i is: 0
3 to_i is: 3
3.28437 to_i is: 3

0 comments on commit 158ca99

Please sign in to comment.