From: Angus Hammond Date: 2010-06-06T16:59:10+09:00 Subject: Re: Please explain this "Why's" example please First things first there wouldn't be an entry box and an ok button, it would be on a command line (just text). gets is a method which when run will wait until the user enters something and then presses enter and then it returns what the user enters (so in the example what you enter gets stored in idea (which is a variable). each is a very useful method. In this example I assume code_words is a hash (you put lots of info in the hash, and give each piece of data a key so you can access it which is more data). For each piece of data the code between each and end is run (in this case idea.gsub!(real,code)) with real being the key and code being the data. Example: If code_words was {"world"=>"planet", "hello"=>"hi"} and you input hello world then idea would be made to equal hi planet. Weirdly this code doesn't do anything with the idea once it has changed it but I assume that this is not the whole code or something. On 06/06/10 08:20, Kaye Ng wrote: > I'm reading Why's Poignant Guide to Ruby. > > I don't understand this example. > -------------------------------------------------------- > require 'wordlist' > > # Get evil idea and swap in code words > > print "Enter your new idea: " > > idea = gets > > code_words.each do |real, code| > idea.gsub!( real, code ) > end > --------------------------------------------------------- > I'm assuming that, if I run the program, there would be a message on the > screen that says "Enter your new idea:" and a text box for me to type > something. > > After I type a word (and press an 'OK' button?), then what? Can anyone > explain to me the role of Gets here? And also the Each block please. You > may also give me another example with Gets and Each. > > Explain it to me as if I'm a moron please. No big words, no complicated > programming terms, none of that. I'm a beginner. > > Thanks so much for your time. =) >