[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: gets

jabowen

7/13/2007 1:32:00 PM

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 <ja_bowen@yahoo.com> 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
>
>
>
>
____________________________________________________________________________________Ready
> for the edge of your seat?
> Check out tonight's top picks on Yahoo! TV.
> http://tv....
>
>



____________________________________________________________________________________
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invi...


2 Answers

John Joyce

7/13/2007 4:14:00 PM

0


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 <ja_bowen@yahoo.com> 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

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.


John Joyce

7/14/2007 2:47:00 AM

0


On Jul 13, 2007, at 5:38 PM, Logan Capaldo wrote:

> On 7/13/07, John Joyce <dangerwillrobinsondanger@gmail.com> 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 <ja_bowen@yahoo.com> 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