# Help a noob

**URL:** <https://www.ruby-forum.com/t/help-a-noob/185728>\
**Category:** Ruby\
**Created:** [March 25, 2010, 7:08am UTC](https://www.ruby-forum.com/t/help-a-noob/185728 "2010-03-25T07:08:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 25, 2010, 7:08am UTC](https://www.ruby-forum.com/t/help-a-noob/185728/1 "2010-03-25T07:08:45Z")

</div>

Ok, I have been programming for two days now. Ruby is my first  
language ever. But all the books I look for already assume you have a  
good deal of knowledge in programming. Does anyone know where I can get  
a good basic start that will give me lots of examples? Also if it is  
possible can someone please tell me what I am doing wrong? This is my  
first program so please do not make too much fun of me. Thanks yall.

-Mehektet-

This is my program…

# Program will ask for a persons personal information and then display

# the results on the screen. Finally it will ask a person to give their

# favourite number and then add it to their age and suggest a new

# favourite number.

puts ‘What is your first name?’  
Fname = gets.chomp  
puts ‘What is your middle name?’  
Mname = gets.chomp  
puts ‘What is your last name?’  
Lname = gets.chomp

puts ‘’

puts ‘What is your age?’  
Age = gets.chomp

puts ‘’

puts ‘What is your favourite number?’  
Fnum = gets.chomp

puts ’ ’

puts ‘This is your information…’  
puts 'You are ’ +Fname+ ’ ’ +Mname+ ’ ’ +Lname+ ‘.’  
puts 'Your age is ’ +Age+ ‘.’  
puts 'And your favourite number is ’ +Fnum+ ‘.’

C = Fnum.to\_i  
A = Age.to\_i

NFnum = C + A

puts 'Maybe, your favourite number should be ’ +NFnum+ ‘.’

---

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 25, 2010, 8:39am UTC](https://www.ruby-forum.com/t/help-a-noob/185728/2 "2010-03-25T08:39:42Z")

</div>

Correct the last line as follows:

puts 'Maybe, your favourite number should be ’ + NFnum.to\_s + ‘.’

that is , convert an integer to a string with ‘to\_s’ before adding to  
another string.

Also, it is a convention to use small letters for simple variable names,  
so  
change the names like Fname as follows:  
Fname to f\_name or first\_name

You can search for on line tutorials to start with.

Prasad

2010/3/25 Omar V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 25, 2010, 9:07am UTC](https://www.ruby-forum.com/t/help-a-noob/185728/3 "2010-03-25T09:07:48Z")

</div>

On Thu, Mar 25, 2010 at 12:08 AM, Omar V.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Mname = gets.chomp  
> puts ‘What is your favourite number?’  
> C = Fnum.to\_i  
> A = Age.to\_i
> 
> NFnum = C + A
> 
> ## puts 'Maybe, your favourite number should be ’ +NFnum+ ‘.’
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi, Omar.

As you have realized, you need to convert the the age and Fnum to  
integers  
in order to add them. Now that you want to add them to the strings,  
though,  
you need to convert them back to strings. You can do this with the to\_s  
method. So you should change  
puts 'Maybe, your favourite number should be ’ +NFnum+ ‘.’  
into  
puts 'Maybe, your favourite number should be ’ +NFnum.to\_s+ ‘.’

* * *

A few suggestions for you:

In Ruby, constants begin with uppercase letters, and variables begin  
with  
lowercase letters. While they don’t change in this particular program,  
the  
name and age and so on are really more variables than constants. Maybe  
the  
user has a birthday, or gets married and changes their surname. So I  
would  
use lowercase variables here. For example:  
puts ‘What is your first name?’  
fname = gets.chomp

Personally, I’d even call it first\_name because you’re going to have to  
read  
it later. Since Ruby is dynamically typed, descriptive variable names  
can  
help you avoid a lot of errors, as they imply what kind of data they  
contain. Fnum, for example, doesn’t mean anything to me until I read the  
code to see how it is used. But favourite\_num is pretty fairly forward.  
If  
you don’t like the additional typing, a good text editor should do  
completion for you, where you type the firs few characters and press a  
key  
to have it figure out what name you are writing and complete it for you.

I would store the numeric variables as numbers. That is really what they  
are, and you shouldn’t store them as something different just because  
that  
is the format you received them in. You can see that it even created a  
headache for you, having to create two new variables to hold the integer  
versions (that is also partially due to using constants).  
So I would change  
puts ‘What is your favourite number?’  
Fnum = gets.chomp  
into  
puts ‘What is your favourite number?’  
favourite\_num = gets.chomp.to\_i

Regarding the above example, to\_i will convert a string to an integer,  
up  
until it finds a non numeric character. You use .chomp to remove the  
newline, but to\_i will stop there automatically, so it can be simplified  
further with  
puts ‘What is your favourite number?’  
favourite\_num = gets.to\_i

You are creating your string by concatenating. That is just a fancy way  
of  
saying that you use the plus sign 🙂 In most cases with Ruby, it is  
preferable to use interpolation, which is a fancy way of saying that you  
embed the value inside the string rather than adding it to the string.  
This  
is a little bit more efficient, and most of the time is easier to read.  
It  
also has the nice advantage of calling the to\_s method for you on  
whatever  
it receives, so, for example, where you have Fnum.to\_s, you could just  
leave  
as Fnum. The interpolated version would look like this  
puts 'Maybe, your favourite number should be ’ +NFnum.to\_s+ ‘.’  
becomes  
puts “Maybe, your favourite number should be #{NFnum}.”  
You need to use double quotes when interpolating, and wherever you have  
#{…} inside of it, that is a little area where you can place your  
code.

This is just a style thing, and not necessarily an agreed upon community  
conclusion, but I would put the print statement and the gets statement  
on  
the same line, and split them with a semicolon. A semicolon tells Ruby  
that  
you are writing a new line, even though all the code is on the same  
line. So  
I would change  
puts ‘What is your first name?’  
Fname = gets.chomp  
into  
puts ‘What is your first name?’ ; Fname = gets.chomp  
When you get comfortable enough to write functions, try writing one  
where  
you submit the string to be printed, and receive back the variable the  
user  
answered with.

To write a newline, you can just say puts, you don’t need the empty  
string  
in there.

I don’t know how long it took you to write this, but if you ran it any  
decent number of times, you probably sat there tediously entering your  
information over and over again. That can get frustrating, but there is  
a  
fairly easy way to get around such things (note this is not a widely  
used  
Ruby practice, and I’m not sure others would sanction it, but I find it  
a  
nice way to save time). In Ruby, at the end of your file, you can write  
**END** and then you can put data down there. That data is accessible to  
your program like a file inside the constant DATA. The method .gets  
pulls  
it’s data from the global variable $stdin, you can assign DATA to $stdin  
so  
that it instead pulls from the data that you have entered at the end of  
your  
file. This means that you can code in the values you used to have to  
enter  
by hand. Then later, when you want to switch it back, just comment out  
the  
line assigning DATA to $stdin, and it will pull from the actual standard  
input, as it currently does, rather than the data at the end of your  
file.  
$stdin = DATA  
puts “your name is #{gets.chomp} #{gets.chomp} #{gets.chomp}”  
puts “you are #{gets.to\_i} years old”  
puts “your favourite number is #{gets.to\_i}”  
**END**  
John  
James  
Doe  
25  
12

After you get more comfortable, you should move to actual testing 🙂 But  
this is a nice simple way to automate input when you just want to test  
out  
an idea real fast. You can also do this from the command line, if you  
are  
using Mac or Linux, by writing that data in a file and using the  
redirect  
operator, which would look like this.  
$ruby my\_program.rb \< my\_data.txt

That is pretty much everything I can think of, so if you did everything  
above, your program might look like this.  
[http://pastie.org/private/g4ulbnq2caxjdag5aipnag](http://pastie.org/private/g4ulbnq2caxjdag5aipnag)

---

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 25, 2010, 9:20am UTC](https://www.ruby-forum.com/t/help-a-noob/185728/4 "2010-03-25T09:20:41Z")

</div>

On Thu, Mar 25, 2010 at 11:38 AM, Omar V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ok, I have been programming for two days now. Ruby is my first  
> language ever. But all the books I look for already assume you have a  
> good deal of knowledge in programming. Does anyone know where I can get  
> a good basic start that will give me lots of examples?

Chris P.'s “Learn To Program” is great. The main thing is not to  
skip any of the exercises.

[http://pine.fm/LearnToProgram/](http://pine.fm/LearnToProgram/)

martin

---

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 25, 2010, 9:12am UTC](https://www.ruby-forum.com/t/help-a-noob/185728/5 "2010-03-25T09:12:29Z")

</div>

On Thu, Mar 25, 2010 at 7:08 AM, Omar V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ok, I have been programming for two days now. Ruby is my first  
> language ever.

Welcome ! I hope you enjoy it.

> But all the books I look for already assume you have a  
> good deal of knowledge in programming. Does anyone know where I can get  
> a good basic start that will give me lots of examples?

I’ve seen people telling good things about Chris P.s’ book:  
[http://pine.fm/LearnToProgram/](http://pine.fm/LearnToProgram/)  
although I haven’t read it.

> Also if it is  
> possible can someone please tell me what I am doing wrong? This is my  
> first program so please do not make too much fun of me. Thanks yall.
> 
> This is my program…
> 
> # Program will ask for a persons personal information and then display
> 
> # the results on the screen. Finally it will ask a person to give their
> 
> # favourite number and then add it to their age and suggest a new
> 
> # favourite number.

Your program looks fine. The only comments are idiomatic stuff:

> puts ‘What is your first name?’  
> Fname = gets.chomp

In Ruby the convention is to use snake\_case. Also names that start  
with an uppercase letter are constants, so I’d do:

first\_name = gets.chomp # also, don’t be afraid to have longer  
variable names if they are clearer

> puts ‘What is your middle name?’  
> Mname = gets.chomp

middle\_name = gets.chomp

> puts ‘What is your last name?’  
> Lname = gets.chomp

last\_name = gets.chomp

> puts ‘’

puts #no need to pass an empty string

> puts ‘What is your age?’  
> Age = gets.chomp

age = gets.chomp

> puts ‘’

puts

> puts ‘What is your favourite number?’  
> Fnum = gets.chomp

favourite\_number = gets.chomp

Also, my preference is to transform strings into integers the earliest  
possible if the concept is really a number so, I’d do:

age = gets.chomp.to\_i # or Integer(gets.chomp) for more strict  
tranformation  
favourite\_number = gets.chomp.to\_i

> puts ’ ’

puts

> puts ‘This is your information…’  
> puts 'You are ’ +Fname+ ’ ’ +Mname+ ’ ’ +Lname+ ‘.’  
> puts 'Your age is ’ +Age+ ‘.’  
> puts 'And your favourite number is ’ +Fnum+ ‘.’

String interpolation is preferred to concatenation (less objects to  
create):

puts “This is your information…”  
puts “You are #{first\_name} #{middle\_name} #{last\_name}.”  
puts “Your age is #{age}.”  
puts “And your favourite number is #{favourite\_number}”

> C = Fnum.to\_i  
> A = Age.to\_i

not needed anymore

> NFnum = C + A

new\_favourite\_number = favourite\_number + age

> puts 'Maybe, your favourite number should be ’ +NFnum+ ‘.’

puts “Maybe, your favourite number should be #{new\_favourite\_number}”

which by the way removes the problem you were having, since string  
interpolation calls to\_s automatically.

Jesus.
