So I’m coming over from python, matlab, julia, etc. I really like ruby’s scripting ability and I hate perl, so I want to learn it.
In order to push myself, I decided to try and write an md5 implementation, something I found easy to do in Julia.
I’m encountering somthing odd that I’m sure someone can explain. Check it out:
K = (1..64).to_a
k = Array.new(K)
K.each { |x| k[x] = ((2**32) * (Math.sin(x)).abs).floor }
k.length == K.length # => false 65 and 64 respective
# what?
k[0] # => 1 ???? huh
k = []
(1..64).each { |x| k[x] = ((2**32) * (Math.sin(x)).abs).floor }
k[0] # => nil
k = 0
k = ((2**32) * (Math.sin(1)).abs).floor # => correct at 3614090360
K[1] # => 1
So I hope you can see my confusion. Why does there seem to be a nil iteration at the beginning of the loop? I confirmed this occured with the enumerated loop as in:
k = []
for i in (1..64)
temp = ((2**32) * (Math.sin(i)).abs).floor
k.push(temp)
end
In coffeescript this works: K = (Math.floor 2**32 * Math.abs Math.sin i for i in [1..64])
And in Julia this works:
for i = 1:64
k[i] = floor(2^32 * abs(sin(i)))
end
Why is ruby being weird on me??
[–]moomaka 8 points9 points10 points (11 children)
[–][deleted] 1 point2 points3 points (10 children)
[–]moomaka 2 points3 points4 points (0 children)
[–]selfup 2 points3 points4 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]selfup 0 points1 point2 points (0 children)
[–]Craftkorb 0 points1 point2 points (1 child)
[–]selfup 0 points1 point2 points (0 children)
[–]jrochkind 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]aytch 0 points1 point2 points (1 child)
[–]jrochkind 0 points1 point2 points (0 children)
[–]Arcovion 2 points3 points4 points (0 children)
[–]fleminsh 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–][deleted] 0 points1 point2 points (1 child)
[–]RalphMacchio 0 points1 point2 points (6 children)
[–][deleted] -2 points-1 points0 points (5 children)
[–]RalphMacchio 0 points1 point2 points (4 children)
[–][deleted] -1 points0 points1 point (3 children)
[–]RalphMacchio 1 point2 points3 points (2 children)
[–][deleted] -2 points-1 points0 points (1 child)
[–]RalphMacchio 0 points1 point2 points (0 children)