Learn Ruby the Hard Way – Exercise 12 Prompting People for Numbers

some useful things in Ruby I’ve discovered:
– Find your previous code cluttering up the Terminal window? Use ctrl + l to clear the window and start fresh!
– Want to break up a long string into multiple lines? (Without breaking up the output with line breaks.) Put a forward slash \ at the end of each line.

Here is  my work for Exercise 12:

Exercise 12 code
Exercise 12 code
Exercise 12 output
Exercise 12 output

Study Drills:
1. Try out the .to_f operation. What does .to_f do?
The .to_f method on a string returns the interpretation of the leading characters as a floating point number. Characters past the valid number characters are ignored. If no valid number is found at the beginning of the string, 0.0 will be returned.

Playing around with .to_f
Playing around with .to_f

2. To play with .to_f more, make a small script that asks for some money and gives back 10% of it. If I give your script 103.4 (dollars), your script gives me back 10.34 in change.

Giving back 10%
Giving back 10%
Code output, using 103.4 and 250.40 as inputs for amount.
Code output, using 103.4 and 250.4 as inputs for amount.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s