# Issues from an extreme beginner

**URL:** <https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468>\
**Category:** Ruby\
**Created:** [February 7, 2013, 5:08am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468 "2013-02-07T05:08:23Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 5:08am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/1 "2013-02-07T05:08:23Z")

</div>

Hey guys, great to be part of such a great community! I look forward to  
learning Ruby alongside all of you!

I’ve very recently picked up ruby and have been reading a book called  
“Learn to Program”(2005) by Chris P… Basically, it’s a hands on guide  
to Ruby. So I’ve been going through the basic programs with no problem,  
up until now. I’ve been trying at a program for about an hour now, to no  
prevail and was hoping you guys could lend me a hand.

The program asks the user to input their first, middle and last names in  
separate strings and displays how many characters are in the combined  
three names, in theory. I have no issues obtaining the string and  
displaying them, but somewhere along the path of converting the string  
to an integer I get messed up.

Now, I’ll share some versions of this failed program that have yielded  
errors (dashed line’s indicate another program):

I get the “Can’t convert Fixnum into string (Type error)” for the first  
two

* * *

puts ‘Enter your first, middle and last name:’  
name1 = gets.chomp  
name2 = gets.chomp  
name3 = gets.chomp  
names1 = name1.length  
names2 = name2.length  
names3 = name3.length  
puts ‘There are ’ + names1.to\_i + names2.to\_i + names3.to\_i  
puts ’ character’s in your name!’

* * *

puts ‘Enter your first, middle and last name:’  
name1 = gets.chomp  
name2 = gets.chomp  
name3 = gets.chomp  
puts ‘There are ’ + (name1.length).to\_i + (name2.length).to\_i +  
(name3.length).to\_i  
puts ’ character’s in your name!’

* * *

And I also had another one (I must have accidently overwritten it) that  
converted the strings to integers, by the “.length” method, and back to  
strings and added them. Which yielded no error, but instead simply added  
the string numbers together (5+6+4=564 instead of 5+6+4=15)

I was just wondering if somebody could take a look at this code and  
point me in the right direction.

Much appreciated,  
Bruce

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 7:12am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/2 "2013-02-07T07:12:06Z")

</div>

Here’s a small hint:

(((str + int =\> str) + int =\> str) + int =\> str)

(str + (int + (int + int =\> int) =\> int) =\> str)

Also, String#length returns an int, so you should never need to call  
.to\_i  
on it.

Sent from my phone, so excuse the typos.

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 7:15am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/3 "2013-02-07T07:15:05Z")

</div>

Matthew K. wrote in post #1095671:

> Here’s a small hint:
> 
> (((str + int =\> str) + int =\> str) + int =\> str)
> 
> (str + (int + (int + int =\> int) =\> int) =\> str)
> 
> Also, String#length returns an int, so you should never need to call  
> .to\_i  
> on it.
> 
> Sent from my phone, so excuse the typos.

Er, that’s a conceptual hint, by the way. You can’t actually add an int  
to a string.

(However, `str + int.to_s` works fine…)

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 7:43am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/4 "2013-02-07T07:43:05Z")

</div>

Ahh, thank you Matthew! That was just the push I needed!

I don’t know what I was thinking before, brain fart perhaps, but I was  
unnecessarily converting back and forth between string and integer.

Anyway, this is the winning program:

puts ‘Enter your first, middle and last name:’  
name1 = gets.chomp  
name2 = gets.chomp  
name3 = gets.chomp  
names = name1.length + name2.length + name3.length  
puts ‘There are ’ + names.to\_s + ’ character’s in your name!’

* * *

Now onto greater obstacles! Once more, thanks Matthew!

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 8:11am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/5 "2013-02-07T08:11:06Z")

</div>

On Thu, Feb 7, 2013, at 7:43, Bruce P. wrote:

> puts ‘There are ’ + names.to\_s + ’ character's in your name!’

puts “There are #{names} character's in your name!”

is more efficient imho.

Best Regards:

Zoltan

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 8:55am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/6 "2013-02-07T08:55:26Z")

</div>

Harry, Zoltan: Thank you both! This book that I’m reading is a pretty  
barebones, basic introduction to the Ruby language.

Shortcuts and different approaches are always as helpful as anything!

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 7:44am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/7 "2013-02-07T07:44:49Z")

</div>

> And I also had another one (I must have accidently overwritten it) that  
> converted the strings to integers, by the “.length” method, and back to  
> strings and added them. Which yielded no error, but instead simply added  
> the string numbers together (5+6+4=564 instead of 5+6+4=15)
> 
> I was just wondering if somebody could take a look at this code and  
> point me in the right direction.

A hint.

p “Bruce”.length.class #\> Fixnum  
p (1+2+3).to\_s.class #\> String

Harry

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 10:02am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/8 "2013-02-07T10:02:02Z")

</div>

FYI, you don’t need to escape the single quote when inside double  
quotes.  
“’” is the same as “’”

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 4:26pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/9 "2013-02-07T16:26:49Z")

</div>

On Wed, Feb 6, 2013 at 10:43 PM, Bruce P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I don’t know what I was thinking before, brain fart perhaps, but I was  
> unnecessarily converting back and forth between string and integer.

> names = name1.length + name2.length + name3.length

Or just: (name1 + name2 + name3).length  
Or: [name1, name2, name3].join.length

> puts ‘There are ’ + names.to\_s + ’ character's in your name!’

Uh, there’s no apostrophe in the plural of “character”. FWIW.  
(Sorry, personal pet peeve.)

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 4:34pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/10 "2013-02-07T16:34:39Z")

</div>

“Hajdú Zoltán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #1095676:

> On Thu, Feb 7, 2013, at 7:43, Bruce P. wrote:
> 
> > puts ‘There are ’ + names.to\_s + ’ character's in your name!’
> 
> puts “There are #{names} character's in your name!”
> 
> is more efficient imho.
> 
> Best Regards:
> 
> Zoltan

This is fine one 🙂

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 10:12pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/11 "2013-02-07T22:12:08Z")

</div>

I should have spotted that :o  
I guess I was thinking in strings rather than sentences 🙂

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 9:39pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/12 "2013-02-07T21:39:13Z")

</div>

Hassan S. wrote in post #1095774:

> On Wed, Feb 6, 2013 at 10:43 PM, Bruce P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I don’t know what I was thinking before, brain fart perhaps, but I was  
> > unnecessarily converting back and forth between string and integer.
> 
> > names = name1.length + name2.length + name3.length
> 
> Or just: (name1 + name2 + name3).length  
> Or: [name1, name2, name3].join.length
> 
> > puts ‘There are ’ + names.to\_s + ’ character's in your name!’
> 
> Uh, there’s no apostrophe in the plural of “character”. FWIW.  
> (Sorry, personal pet peeve.)

I caught this after doing it in, maybe 6 of my prior programs.

I’m surprised nobody caught that sooner!

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 10:38pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/13 "2013-02-07T22:38:04Z")

</div>

Am 07.02.2013 07:43, schrieb Bruce P.:

> name3 = gets.chomp  
> names = name1.length + name2.length + name3.length  
> puts ‘There are ’ + names.to\_s + ’ character's in your name!’

You should make it a habit from the start to use more accurate  
variable names, like

characters = name1.length + name2.length + name3.length

# or: character\_number, total\_characters, total\_character\_number, …

puts “There are #{characters} characters in your name!”

stomar

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 10:35pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/14 "2013-02-07T22:35:43Z")

</div>

On 8 February 2013 06:39, Bruce P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Or: [name1, name2, name3].join.length
> > 
> > > puts ‘There are ’ + names.to\_s + ’ character's in your name!’
> > 
> > Uh, there’s no apostrophe in the plural of “character”. FWIW.  
> > (Sorry, personal pet peeve.)
> 
> I caught this after doing it in, maybe 6 of my prior programs.
> 
> I’m surprised nobody caught that sooner!

Well, we’re not marking your assignments for you. 😉

Here is another approach that is Rubyish, albeit slightly excessive:

```
[name1, name2, name3].inject(0) {|sum, name| sum + name.length }

```

–  
Matthew K., B.Sc (CompSci) (Hons)  
[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)  
ABN: 59-013-727-651

“You’ll never find a programming language that frees  
you from the burden of clarifying your ideas.” - xkcd

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 8, 2013, 12:40am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/15 "2013-02-08T00:40:48Z")

</div>

on the side-track in case the name has only one character then the word  
‘character’ is usually written as character(s) rather than as an  
apostrophe  
this visible abbreviation is a valid way to create two options.  
to write it this way will (s) need to be escaped?  
for singular and plural to be viable alternatives?  
can (s) be escaped by either “(s)” or " " ?  
puts needs singular and plural pronouns and tense  
to print ‘there is one ,’ or else put ‘there are…’

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 8, 2013, 1:11am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/16 "2013-02-08T01:11:13Z")

</div>

On Thu, Feb 7, 2013 at 3:40 PM, Timothy G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> on the side-track in case the name has only one character then the word  
> ‘character’ is usually written as character(s) rather than as an apostrophe  
> this visible abbreviation is a valid way to create two options.  
> to write it this way will (s) need to be escaped?  
> for singular and plural to be viable alternatives?  
> can (s) be escaped by either “(s)” or " " ?  
> puts needs singular and plural pronouns and tense  
> to print ‘there is one ,’ or else put ‘there are..’

In a pry console, including one Rails file:

1.9.3 (main):0 \> require ‘active\_support/core\_ext/string/inflections’  
=\> true  
1.9.3 (main):0 \> count = 1  
=\> 1  
1.9.3 (main):0 \> “there #{‘is’.pluralize(count)} #{count}  
#{‘character’.pluralize(count)}.”  
=\> “there is 1 character.”  
1.9.3 (main):0 \> count = 3  
=\> 3  
1.9.3 (main):0 \> “there #{‘is’.pluralize(count)} #{count}  
#{‘character’.pluralize(count)}.”  
=\> “there is 3 characters.”

!! oops, that’s a surprising omission, but

1.9.3 (main):0 \> ActiveSupport::Inflector.inflections{|inflect|  
inflect.plural “is”, “are” }  
…  
1.9.3 (main):0 \> “there #{‘is’.pluralize(count)} #{count}  
#{‘character’.pluralize(count)}.”  
=\> “there are 3 characters.”

HTH,

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 7, 2013, 10:41pm UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/17 "2013-02-07T22:41:35Z")

</div>

Am 07.02.2013 22:25, schrieb Matthew K.:

> Here is another approach that is Rubyish, albeit slightly excessive:
> 
> ```
> [name1, name2, name3].inject(0) {|sum, name| sum + name.length }
> 
> ```

IMHO maybe a little too cryptic for a complete newbie.

stomar

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 8, 2013, 5:01am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/18 "2013-02-08T05:01:39Z")

</div>

hay that is concise and explicit many thanks  
now i am reading docs about [//inflections.rb](https://inflections.rb)  
i grasp your steps under the heading  
“String inflections define new methods on the String class”  
where ‘…/inflections.rb’ has fifteen methods  
your solution shows some neat ways it can be adapted  
in our case both numbers and strings are in singular and plural  
thankfully the output grammar can be corrected to suit either  
alternative  
this was a hitch that is solved in ruby  
it appears quite a fiddly issue more than it really is

On Fri, Feb 8, 2013 at 11:10 AM, Hassan S. \<

---

<div class="post-metadata">

**Author:** ![abruceperson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@abruceperson](https://www.ruby-forum.com/u/abruceperson)\
**Post date:** [February 8, 2013, 7:01am UTC](https://www.ruby-forum.com/t/issues-from-an-extreme-beginner/229468/19 "2013-02-08T07:01:26Z")

</div>

Am 08.02.2013 00:40, schrieb Timothy G.:

> on the side-track in case the name has only one character then the word  
> ‘character’ is usually written as character(s) rather than as an apostrophe  
> this visible abbreviation is a valid way to create two options.  
> to write it this way will (s) need to be escaped?  
> for singular and plural to be viable alternatives?  
> can (s) be escaped by either “(s)” or " " ?  
> puts needs singular and plural pronouns and tense  
> to print ‘there is one ,’ or else put ‘there are..’

1. You do not have to escape `(' and `)’ in single or double  
quoted strings.

2. Generally, you can handle “decisions” with a conditional expression:

You should first learn these basic language features before  
playing around with Rails libraries.
