I have some time before work so want to keep studying!

Time for Exercise 33: While Loops

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
i = 0 numbers = [] while i < 6 puts "At the top is #{i}" numbers.push(i) i += 1 puts "Numbers now:", numbers puts "At the bottom i is #{i}" end puts "The numbers:" # remember you can write this 2 other ways? numbers.each{ |num| puts num } |

And the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex33.rb

At the top is 0

Numbers now:

0

At the bottom i is 1

At the top is 1

Numbers now:

0

1

At the bottom i is 2

At the top is 2

Numbers now:

0

1

2

At the bottom i is 3

At the top is 3

Numbers now:

0

1

2

3

At the bottom i is 4

At the top is 4

Numbers now:

0

1

2

3

4

At the bottom i is 5

At the top is 5

Numbers now:

0

1

2

3

4

5

At the bottom i is 6

The numbers:

0

1

2

3

4

5

And on to the study drills:

**1. Convert this while-loop to a function that you can call, and replace 6 in the test (i < 6) with a variable.**

This was hard. I ended up writing it through twice and having to go back to read the function lessons again. My first try I got this error:

1 2 3 |
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb studydrill33.rb:1:in `loop1': wrong number of arguments (0 for 1) (ArgumentError) from studydrill33.rb:16:in `<main>' |

Basically, it tells me I was not passing in the correct number of arguments. I was trying to define i in the function. I took that out and then called the arguments correctly, which looks like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def loop1(i) numbers = [] widget = 6 while i < widget puts "At the top is #{i}" numbers.push(i) i += 1 puts "Numbers now:", numbers puts "At the bottom i is #{i}" end end loop1(0) |

And to my surprise it works! This is so important to me. I have had doubts that I was just mimicing what I read and not really learning. I had that initial feeling of “oh crap,” seeing this study drill. I then slowed down and thought through it. I read back over past lessons. I knew I had to use def and then just write inside it the while loop. From there I tweaked until it worked. And it did!

The results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb

At the top is 0

Numbers now:

0

At the bottom i is 1

At the top is 1

Numbers now:

0

1

At the bottom i is 2

At the top is 2

Numbers now:

0

1

2

At the bottom i is 3

At the top is 3

Numbers now:

0

1

2

3

At the bottom i is 4

At the top is 4

Numbers now:

0

1

2

3

4

At the bottom i is 5

At the top is 5

Numbers now:

0

1

2

3

4

5

At the bottom i is 6

**2. Use this function to rewrite the script to try different numbers.**

The great thing with functions is that you can quickly change the arguments you pass to them. I went ahead and added a second argument to the function, removing widgets from inside it and passing it through in an argument. I then tried it and here is the result:

The code:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
def loop1(i, widget) numbers = [] while i < widget puts "At the top is #{i}" numbers.push(i) i += 1 puts "Numbers now:", numbers puts "At the bottom i is #{i}" end end loop1(3, 20) |

Here is the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb

At the top is 3

Numbers now:

3

At the bottom i is 4

At the top is 4

Numbers now:

3

4

At the bottom i is 5

At the top is 5

Numbers now:

3

4

5

At the bottom i is 6

At the top is 6

Numbers now:

3

4

5

6

At the bottom i is 7

At the top is 7

Numbers now:

3

4

5

6

7

At the bottom i is 8

At the top is 8

Numbers now:

3

4

5

6

7

8

At the bottom i is 9

At the top is 9

Numbers now:

3

4

5

6

7

8

9

At the bottom i is 10

At the top is 10

Numbers now:

3

4

5

6

7

8

9

10

At the bottom i is 11

At the top is 11

Numbers now:

3

4

5

6

7

8

9

10

11

At the bottom i is 12

At the top is 12

Numbers now:

3

4

5

6

7

8

9

10

11

12

At the bottom i is 13

At the top is 13

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

At the bottom i is 14

At the top is 14

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

14

At the bottom i is 15

At the top is 15

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

14

15

At the bottom i is 16

At the top is 16

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

14

15

16

At the bottom i is 17

At the top is 17

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

At the bottom i is 18

At the top is 18

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

At the bottom i is 19

At the top is 19

Numbers now:

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

At the bottom i is 20

I am amazed that I am finally getting this more and more. Yay!

**3. Add another variable to the function arguments that you can pass in that lets you change the + 1 on line 8 so you can change how much it increments by. and 4. Rewrite the script again to use this function to see what effect that has.**

The code:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
def loop1(i, widget, increment) numbers = [] while i < widget puts "At the top is #{i}" numbers.push(i) i += increment puts "Numbers now:", numbers puts "At the bottom i is #{i}" end end loop1(3, 20, 4) |

The result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb

At the top is 3

Numbers now:

3

At the bottom i is 7

At the top is 7

Numbers now:

3

7

At the bottom i is 11

At the top is 11

Numbers now:

3

7

11

At the bottom i is 15

At the top is 15

Numbers now:

3

7

11

15

At the bottom i is 19

At the top is 19

Numbers now:

3

7

11

15

19

At the bottom i is 23

And that is all the time I have. I will continue at the end of this exercise when I return later! So excited!