# Uso di gets

**URL:** <https://www.ruby-forum.com/t/uso-di-gets/148836>\
**Category:** IT\
**Created:** [September 17, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/uso-di-gets/148836 "2008-09-17T16:44:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mentat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@mentat](https://www.ruby-forum.com/u/mentat)\
**Post date:** [September 17, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/uso-di-gets/148836/1 "2008-09-17T16:44:20Z")

</div>

Salve a tutti!!!  
La domanda e’ la seguente:  
Perche’ se scrivo  
…  
n=6  
def fact(n)  
if n==0  
1  
else  
n_fact(n-1)  
end  
end  
puts fact(n)  
…  
Mi da’ il risultato giusto.Mentre se scrivo:  
…  
puts ‘Scrivi un numero’  
puts ’ ’  
n=gets  
def fact(n)  
if n==0  
1  
else  
n_fact(n-1)  
end  
end  
puts fact(n)  
…  
Mi da’ il seguente errore:  
undefined method `-’ for #String:0x3468f4 (NoMethodError)  
Dove e’ che sbaglio?

Grazie dell’eventuale risposta.

---

<div class="post-metadata">

**Author:** ![mentat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@mentat](https://www.ruby-forum.com/u/mentat)\
**Post date:** [September 17, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/uso-di-gets/148836/2 "2008-09-17T16:58:23Z")

</div>

> puts fact(n)  
> …  
> Mi da’ il seguente errore:  
> undefined method `-’ for #String:0x3468f4 (NoMethodError)  
> Dove e’ che sbaglio?

Prova a fare un cast da stringa ad intero simile a questo.

n = gets.to\_i  
def fact(n)  
if n==0  
…

Bye!

---

<div class="post-metadata">

**Author:** ![mentat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@mentat](https://www.ruby-forum.com/u/mentat)\
**Post date:** [September 17, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/uso-di-gets/148836/3 "2008-09-17T17:10:23Z")

</div>

Andrea R. wrote:

> > puts fact(n)  
> > …  
> > Mi da’ il seguente errore:  
> > undefined method `-’ for #String:0x3468f4 (NoMethodError)  
> > Dove e’ che sbaglio?
> 
> Prova a fare un cast da stringa ad intero simile a questo.
> 
> n = gets.to\_i  
> def fact(n)  
> if n==0  
> …
> 
> Bye!

Grazie Andrea. Adesso funziona!  
Ma non capisco perche’ devo fare cosi’…forse perche’ n e’ considerato  
come una string?  
Mi leggero’ meglio i manuali… 🙂  
Ciao e obrigado!!

---

<div class="post-metadata">

**Author:** ![mentat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@mentat](https://www.ruby-forum.com/u/mentat)\
**Post date:** [September 17, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/uso-di-gets/148836/4 "2008-09-17T17:33:02Z")

</div>

2008/9/17 Giovanni C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> […]  
> Ma non capisco perche’ devo fare cosi’…forse perche’ n e’ considerato  
> come una string?

sì, gets restituisce sempre una stringa; se vuoi un numero devi  
convertirla, con .to\_i.

esempio: “3” è una stringa.  
“3”.to\_i dà come risultato 3 (numero).
