Ruby - Enumerable - collect

Sort by

recency

|

99 Discussions

|

  • + 0 comments
    def rot13(secret_messages)
        secret_messages.map {
            |msg| decipher_message(msg)
        }
    end
    
    def decipher_message(msg)
        msg.chars.map { |c|
            case c
                when 'a'..'m', 'A'..'M'
                    (c.ord + 13).chr
                when 'n'..'z', 'N'..'Z'
                    (c.ord - 13).chr
                else
                    c
            end
        }.join
    end
    
  • + 0 comments

    I found that it can be done using .tr ? any ideas?

    def rot13(secret_messages)
       secret_messages.map do |word|
         word.tr("a-z", "n-za-m")
       end
     end
    
  • + 0 comments
    def rot13(secret_messages)
        secret_messages.map do |str|
            str.chars.map{|char| char.ord.between?(65,90) || char.ord.between?(97,122) ? cipher(char) : char }.join
        end
    end
    
    def cipher(char)
        13.times {char.next!}
        return char.size > 1 ? char[1] : char
    end
    
  • + 0 comments
    def rot13(secret_messages)
      secret_messages.map do |string|
        string.chars.map { |c| c.ord < 97 || c.ord > 122 ? c : c.ord + 13 > 122 ? (c.ord - 13).chr : (c.ord + 13).chr }.join
      end
    end
    
  • + 0 comments

    def rot13(secret_messages)

    return secret_messages.map {|d| 
        ans = ""
        for i in(0...d.length)
            ans.insert(i,helper(d[i]))
        end
        ans
        }
    

    end

    def helper(ch) alpb = "abcdefghijklmnopqrstuvwxyz" i = ch.downcase.ord - 'a'.ord return i > 25 || i < 0 ? ch : ch.ord >= 65 && ch.ord <=90 ? alpb[(i + 13)%26].upcase : alpb[(i + 13)%26] end