From: John Joyce Date: 2007-07-14T11:47:10+09:00 Subject: Re: gets On Jul 13, 2007, at 5:38 PM, Logan Capaldo wrote: > On 7/13/07, John Joyce wrote: >> >> >> On Jul 13, 2007, at 8:31 AM, Jeffrey Bowen wrote: >> >> > Thanks >> > Sometime I'm just slow. I tried >> > "test.chomp = gets" but I didn't try "test = >> > gets.chomp". The error message most likely told me >> > what was wrong. >> > >> > Thanks >> > Jeff >> > >> > --- Jeffrey Bowen wrote: >> > >> >> Of course the code below will not work because gets >> >> is >> >> adding /n to the entry. I know that chomp will >> >> remove >> >> the /n but is there away to cut the /n without >> >> reassigning test1 to another variable IE test2 = >> >> test1.comp >> >> >> >> print "test1 " >> >> test1 = gets >> >> puts test1.class >> >> if test1 == "1" >> >> puts "step 1" >> >> else >> >> puts "step 2" >> >> end >> >> >> >> Thanks >> >> Jeff >> >> >> test.chomp = gets >> >> and >> >> test = gets.chomp >> >> Will give you different results. >> test.chomp will return a chomped value but doesn't permanently chomp >> the value in test >> you would need to do >> >> test.chomp! = gets > > > You can't do this. (At least not using that syntax). Furthermore it > would > not be able to have the desired semantics anyway. > > Your choices are > test1 = gets.chomp > > OR > > test1 = gets > test1 = test1.chomp > > OR > > test1 = gets > test1.chomp! > > OR > > test1 = gets.chomp! # will work in this case, not a good idea from > a best > practices POV > > I mighta missed a variation, but you definitely can't do > test1.chomp! = > gets. That's nonsense. > > This is the destructive version of chomp, methods ending with ! >> usually modify the receiver in place, others usually don't. >> The reason my version doesn't need chomp! is because it takes the >> chomped value of gets and assigns that to test. >> >> >> You're right I got ahead of myself while typing! JJ