Reputation: 4996
Is there some method in coffeescript that returns true when an array has some items in it? Like method in ruby present?
:
[].present? false
[1].present? true
According to http://arcturo.github.com/library/coffeescript/07_the_bad_parts.html an array emptiness in coffeescript is determined by its length
alert("Empty Array") unless [].length
that seems so lame to me.
Upvotes: 13
Views: 17800
Reputation: 759
I think using in
works as well.
arr = [1, 2, 3, 4, 5]
a = 1
if a in arr
console.log 'present'
else
console.log 'not present'
Output
$ present
Upvotes: 0
Reputation: 14412
I don't think there is but can be:
Array::present = ->
@.length > 0
if [42].present()
# why yes of course
else
# oh noes
A very simple and incomplete implementation but it should give you some ideas. And for the record, there's no present?
method in Ruby, the method is added by the active_support
gem.
Upvotes: 23
Reputation: 153
Unfortunately, there isn't. The best way to do it is by comparing its length.
Upvotes: 6