How to create 'numbered symbols' in iterator?

I had this

    {
    key0: OpenSSL::Digest::SHA256.new.digest("1 #{key}"),
  key1: OpenSSL::Digest::SHA256.new.digest("2 #{key}"),
  key2: OpenSSL::Digest::SHA256.new.digest("3 #{key}"),
  key3: OpenSSL::Digest::SHA256.new.digest("4 #{key}")
  }

I want this

subkeys = {}
4.times do |i|
subkeys[:key"#{i}"] = OpenSSL::Digest::SHA256.new.digest("#{i} #{key}")
end

you can

subkeys = {}
4.times do |i|
subkeys[:“key#{i}”] = OpenSSL::Digest::SHA256.new.digest("#{i} #{key}")
end

Since a symbol is just an interned string you can use “key#{i}”.intern
(or .to_sym) to cast it to a symbol.

On 29 May 2012 21:28, rooby shoez [email protected] wrote:

subkeys = {}
4.times do |i|
subkeys[:key"#{i}“] = OpenSSL::Digest::SHA256.new.digest(”#{i} #{key}")
end


Posted via http://www.ruby-forum.com/.


Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd

both of those ways give me errors that are NOT present when I use this

  {
  key0: OpenSSL::Digest::SHA256.new.digest("1 #{key}"),
  key1: OpenSSL::Digest::SHA256.new.digest("2 #{key}"),
  key2: OpenSSL::Digest::SHA256.new.digest("3 #{key}"),
  key3: OpenSSL::Digest::SHA256.new.digest("4 #{key}")
  }

the error is: `[]’: can’t convert Symbol into Integer (TypeError)

and it is from here

r = r_operation(subkeys[:key0], l, r)

never mind it is because I didn’t return the hash anymore