test = Hash(Int32, Int32).new
test = 20
test = 50
def max_by_internal_test(&block : Hash(Int32, Int32) -> Hash(Int32, Int32))
each_with_index do |elem, i|
#value = yield elem
max_by_internal_test do |i|
undefined local variable or method ‘each_with_index’
I’m trying to see how this line of code works:
I have specified the input, and output types
Also, what does “forall U” do?
You’re trying to call a Hash method as a method in the top-level namespace. It seems like you figured this out already, though.
forall U specifies a method as generic. In the case of
private def max_by_internal(&block : T -> U) forall U
this means that the method takes a block that takes the generic type T of the Enumerable and returns any type U.
In regards to your playground links (let’s use this one, the issue with yield isn’t that you just can’t use it but that you’re passing to yield a Tuple (which is a Hash key and value) in line 25 and then the block that you’re giving in line 9 just returns that Tuple. Then, in line 26 you’re comparing value, which is a Tuple, to max, which is an Int32. That just doesn’t work. If you instead return, for example, the Hash value, it will run: https://play.crystal-lang.org/#/r/6lrv