# A very early Ruby problem

**URL:** <https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854>\
**Category:** Ruby\
**Created:** [May 23, 2007, 8:11am UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854 "2007-05-23T08:11:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonndailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jonndailey](https://www.ruby-forum.com/u/Jonndailey)\
**Post date:** [May 23, 2007, 8:11am UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854/1 "2007-05-23T08:11:12Z")

</div>

In Chris P.'s book, “Learn to Program”, he asks you to write a small  
app. I’m having trouble writing this app and I was wondering if anyone  
could help me out.

Here’s the excerpt:

“I guess we could write a program which asks for your first, middle,  
and last names individually, and then adds those lengths together…  
hey, why don’t you do that! Go ahead, I’ll wait.”  
[http://pine.fm/LearnToProgram/?Chapter=05](http://pine.fm/LearnToProgram/?Chapter=05)

I’m having a problem writing this code. I know a lot of you will look  
at this and think this is the easiest thing ever. I hope I catch  
someone like that to help me out.

Any ideas?

thanks a lot.

---

<div class="post-metadata">

**Author:** ![Jonndailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jonndailey](https://www.ruby-forum.com/u/Jonndailey)\
**Post date:** [May 23, 2007, 8:49am UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854/2 "2007-05-23T08:49:15Z")

</div>

On 5/23/07, Jonndailey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having a problem writing this code. I know a lot of you will look  
> at this and think this is the easiest thing ever. I hope I catch  
> someone like that to help me out.
> 
> Any ideas?
> 
> thanks a lot.

Hi,

Did you understand the program just before that?  
The one that asked for your full name and printed the number of  
characters.

Would you show how much you have been able to write so far?

Harry

–

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)

---

<div class="post-metadata">

**Author:** ![Jonndailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jonndailey](https://www.ruby-forum.com/u/Jonndailey)\
**Post date:** [May 23, 2007, 10:16am UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854/3 "2007-05-23T10:16:24Z")

</div>

Well this is rather simple. You know how to read from STDIN (= console  
input)? There are several ways like gets or readline. So

surname = readline or surename = gets reads from the console and saves  
the string in the variable surname. You can get the length of a string  
with the length method. So surename.length gives the length of surename  
but realize that gets and readline also are saving the newline feed in  
the variable you have to cut off with chomp or length will always be 1  
to high.

A simple implementation looks like this:

#!/usr/bin/env ruby

$VERBOSE = true

# outputs surname on the screen

puts “surname:”

# reads console input to surname

surname = gets

puts “middlename:”  
middlename = gets

puts “lastname:”  
lastname = gets

puts “total length:”

# adds the lengths of the three strings after they have beeen chomped

p surname.chomp.length+middlename.chomp.length+lastname.chomp.length

Jonndailey wrote:

> I’m having a problem writing this code. I know a lot of you will look  
> at this and think this is the easiest thing ever. I hope I catch  
> someone like that to help me out.
> 
> Any ideas?
> 
> thanks a lot.

–  
greets

```
                 one must still have chaos in oneself to be able to

```

give birth to a dancing star

---

<div class="post-metadata">

**Author:** ![Jonndailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jonndailey](https://www.ruby-forum.com/u/Jonndailey)\
**Post date:** [May 23, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854/4 "2007-05-23T18:52:42Z")

</div>

Thanks a lot. I was going about it the wrong way. It looks so simple  
now. Thank you!

---

<div class="post-metadata">

**Author:** ![Jonndailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jonndailey](https://www.ruby-forum.com/u/Jonndailey)\
**Post date:** [May 23, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854/5 "2007-05-23T18:54:43Z")

</div>

Thanks a lot. I was going about it the wrong way. It looks so simple  
now. Thank you!

---

<div class="post-metadata">

**Author:** ![Jonndailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jonndailey](https://www.ruby-forum.com/u/Jonndailey)\
**Post date:** [May 23, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/a-very-early-ruby-problem/104854/6 "2007-05-23T20:52:59Z")

</div>

On May 23, 2007, at 2:10 AM, Jonndailey wrote:

> In Chris P.'s book, “Learn to Program”, he asks you to write a small  
> app. I’m having trouble writing this app and I was wondering if anyone  
> could help me out.

An excellent book. It’s where I learned to program Ruby!

So you’d first want to ask for them. Remember, variables are stored  
from right to left.

#use gets.chomp to ask for the name. Store the result in the  
appropriate variable.  
first\_name = gets.chomp  
middle\_name = gets.chomp  
last\_name = gets.chomp

#Now we need to add these parts together.

And you’re fabulous puts code goes here.

You probably had trouble with how to ask for it, right? I know that’s  
where I had trouble. puts ing the phrase is just as easy - just puts  
each variable, and separate each with a space.

The blind teaching the blind  
HTH  
---------------------------------------------------------------|  
~Ari  
“I don’t suffer from insanity. I enjoy every minute of it” --1337est  
man alive
