vendredi 7 avril 2017

Reusing a Random Letter (Ruby)

I'm trying to get the code below to randomly generate a letter to begin each word of zip, zap, zop. A different letter should be chosen every time the code is run but each word needs to start with the same letter.

I can't seem to get it to reuse the random letter 3 times.

def generate
    puts "3... 2... 1... IMPROV: (enter any letter)"
        text = gets.chomp.upcase
    puts text + "IP, " + text + "AP, " + text + "OP!"
    puts "My turn!"
    yield
    puts g_l(1) + "IP, " + "#{g_l(1)}AP, " + "#{g_l(1)}OP!" 
end

generate do 
    def g_l(number)
       charset = Array('A'..'Z') - Array["A", "E", "I", "O", "U"]
       Array.new(number) { charset.sample } .join
    end
end

What I'm aiming for: Run 1: HIP, HAP, HOP! Run 2: DIP, DAP, DOP! Run 3: SIP, SAP, SOP!

What I'm getting: Run 1: HIP, ZAP COP Run 2: FIP, WAP, DOP Run 3: RIP, TAP, BOP

I tried for hours troubleshooting and trying to find the result using terms like: generating the same random letter, reusing a random letter, storing a random number etc. If I could get help solving my problem and the means to better researching something like this in the future that would be fantastic.

For those curious this is loosely based on the improv game called... Zip, Zap, Zop!




Aucun commentaire:

Enregistrer un commentaire