Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug with blank #169

Closed
kostya opened this issue Jul 23, 2014 · 1 comment
Closed

bug with blank #169

kostya opened this issue Jul 23, 2014 · 1 comment

Comments

@kostya
Copy link
Contributor

kostya commented Jul 23, 2014

i trying to add blank method, but this is not work for arrays:

crystal -e 'p ([] of Int32).blank?'
false

seems some bug with object inheritance.

diff --git a/src/nil.cr b/src/nil.cr
index a8586b3..4cfa225 100644
--- a/src/nil.cr
+++ b/src/nil.cr
@@ -58,4 +58,8 @@ struct Nil
   def not_nil!
     raise "Nil assertion failed"
   end
+
+  def blank?
+    true
+  end
 end
diff --git a/src/object.cr b/src/object.cr
index 6bfa53b..3cfc4a2 100644
--- a/src/object.cr
+++ b/src/object.cr
@@ -61,6 +61,18 @@ class Object
     self
   end

+  def blank?
+    if self.responds_to?(:empty?)
+      self.empty?
+    else
+      false
+    end
+  end
+
+  def present?
+    !self.blank?
+  end
+
   macro getter(name)
     def {{name.id}}
       @{{name.id}}
@asterite
Copy link
Member

It seems the problem is actually with responds_to? not working well with generic classes:

class Foo(T)
  def bar
    true
  end
end

foo = Foo(Int32).new
puts foo.responds_to?(:bar) #=> false

As I side note, I was quite surprised that self.empty? compiled inside the if self.responds_to?(:empty), but it makes sense: self is a variable and its type is restricted inside the if.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants