Ruby Cipher If statements not working -


i wondering if correct things have done wrong in code. attempting make 1 time pad ruby.

every time run code, , provide input put cipher, returns nil or doesn't return exact amount of characters (i enter in 4 character word , returns 2 characters).

h = {} v = 0  ('a'..'z').each |c|   v+=1   h[c] = v end   puts "provide input:" input = gets input.downcase!   if input.include?("a")      n = h["a"] + rand(26)     puts h.index(n)      end if input.include?("b")     n = h["b"] + rand(26)     puts h.index(n)      end if input.include?("c")     n = h["c"] + rand(26)     puts h.index(n)       end if input.include?("d")     n = h["d"] + rand(26)     puts h.index(n)      end if input.include?("e")     n = h["e"] + rand(26)     puts h.index(n)      end if input.include?("f")     n = h["f"] + rand(26)     puts h.index(n)      end if input.include?("g")     n = h["g"] + rand(26)     puts h.index(n)      end if input.include?("h")     n = h["h"] + rand(26)     puts h.index(n)      end if input.include?("i")     n = h["i"] + rand(26)     puts h.index(n)      end if input.include?("j")     n = h["j"] + rand(26)     puts h.index(n)      end if input.include?("k")     n = h["k"] + rand(26)     puts h.index(n)      end if input.include?("l")     n = h["l"] + rand(26)     puts h.index(n)      end if input.include?("m")     n = h["m"] + rand(26)     puts h.index(n)      end if input.include?("n")     n = h["n"] + rand(26)     puts h.index(n)      end if input.include?("o")     n = h["o"] + rand(26)     puts h.index(n)      end if input.include?("p")     n = h["p"] + rand(26)     puts h.index(n)      end if input.include?("q")     n = h["q"] + rand(26)     puts h.index(n)      end if input.include?("r")     n = h["r"] + rand(26)     puts h.index(n)      end if input.include?("s")     n = h["s"] + rand(26)     puts h.index(n)      end if input.include?("t")     n = h["t"] + rand(26)     puts h.index(n)      end if input.include?("u")     n = h["u"] + rand(26)     puts h.index(n)      end if input.include?("v")     n = h["v"] + rand(26)     puts h.index(n)      end if input.include?("w")     n = h["w"] + rand(26)     puts h.index(n)     end if input.include?("x")     n = h["x"] + rand(26)     puts h.index(n)      end if input.include?("y")     n = h["y"] + rand(26)     puts h.index(n)      end if input.include?("z")     n = h["z"] + rand(26)     puts h.index(n)      end 

what wrong code? there better way can this, and/or there way can make more effective , efficient.

quick final question, there better place run code in textmate on mac running os x 10.9.3?

most recent version:

h = {} v = 0  ('a'..'z').each |c|   v+=1   h[c] = v end   puts "provide input:" input = gets input.downcase! input.each_char {|n| puts n, ' ' }   if input.include?("a")      n = h["a"] + rand(26)     puts h.index(n % 26)      end if input.include?("b")     n = h["b"] + rand(26)     puts h.index(n n % 26)      end if input.include?("c")     n = h["c"] + rand(26)     puts h.index(n % 26)       end if input.include?("d")     n = h["d"] + rand(26)     puts h.index(n % 26)      end if input.include?("e")     n = h["e"] + rand(26)     puts h.index(n)      end if input.include?("f")     n = h["f"] + rand(26)     puts h.index(n % 26)      end if input.include?("g")     n = h["g"] + rand(26)     puts h.index(n % 26)      end if input.include?("h")     n = h["h"] + rand(26)     puts h.index(n % 26)      end if input.include?("i")     n = h["i"] + rand(26)     puts h.index(n % 26)      end if input.include?("j")     n = h["j"] + rand(26)     puts h.index(n % 26)      end if input.include?("k")     n = h["k"] + rand(26)     puts h.index(n)      end if input.include?("l")     n = h["l"] + rand(26)     puts h.index(n % 26)      end if input.include?("m")     n = h["m"] + rand(26)     puts h.index(n % 26)      end if input.include?("n")     n = h["n"] + rand(26)     puts h.index(n % 26)      end if input.include?("o")     n = h["o"] + rand(26)     puts h.index(n % 26)      end if input.include?("p")     n = h["p"] + rand(26)     puts h.index(n % 26)      end if input.include?("q")     n = h["q"] + rand(26)     puts h.index(n % 26)      end if input.include?("r")     n = h["r"] + rand(26)     puts h.index(n % 26)      end if input.include?("s")     n = h["s"] + rand(26)     puts h.index(n % 26)      end if input.include?("t")     n = h["t"] + rand(26)     puts h.index(n % 26)      end if input.include?("u")     n = h["u"] + rand(26)     puts h.index(n % 26)      end if input.include?("v")     n = h["v"] + rand(26)     puts h.index(n % 26)      end if input.include?("w")     n = h["w"] + rand(26)     puts h.index(n % 26)     end if input.include?("x")     n = h["x"] + rand(26)     puts h.index(n % 26)      end if input.include?("y")     n = h["y"] + rand(26)     puts h.index(n % 26)      end if input.include?("z")     n = h["z"] + rand(26)     puts h.index(n % 26)      end 

as understand code, h["z"] == 26. if add random number 26, (let's 4), have 30. h.index(30) nil. wager want h.index(n%26).

edit:

complete code, based partially on other answer given:

h = {} v = 0  ('a'..'z').each |c|     h[c] = v     v+=1 end  puts "provide input:" input = gets  input.chomp.each_char |c|     next if c.upcase !~ /^[a-z]$/     n = h[c.upcase] + rand(26)     puts h.key(n % 26) end 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -