The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
initialize
from now on unless I need to change the allocation strategy.
new
method that prompted this question early today? def self.new(&)
res = new
res.each_index { |i| res[i] = yield i }
res
end
Indexable
I do this to allow passing a block
def self.rand(min : T = 0.0, max : T = 1.0)
new { Random.rand min..max }
end
def self.rand(min : self = ZERO, max : self = ONES)
new { |i| Random.rand min[i]..max[i] }
end
include
vs extend
?
Which makes me curious about this
def something
yield
end
def pass
something { yield }
puts "hey"
end
pass do
break
end
does it print "hey"?
Indexable
instead of x/y/z/w in order to make a mixin that can be added to 2d, 3d and 4d vectors? The short-circuiting behavior of &&
tells me yes def <=>(other : self)
@x <=> other.x && @y <=> other.y
end
each
or something from Indexable
rather than the logic explicitly needing to know about the ivars