Learn to Program

Chapter 6

More About Methods


Mean Mom

This is pretty much the same as in the book. I just added some extras.

  1. #Mean mommy

  2. puts 'You walk into the kitchen, your mother has her back to you.'

  3. puts 'What do you say?'

  4. demand = gets.chomp.upcase

  5. puts 'WADDYA MEAN "' + demand + '"?!? YOU\'RE GROUNDED!'

  6. gets #press enter to exit

Result:

Line Width and Justification

I created a three-column table with only one line of data. I left-justified everything because right-justification seemed a little odd.

I put each line into its own variable and concatenated them just for the purposes of showing it on this web page. I originally had it on one line. Either way is acceptible.

  1. #Line width and justifictaion examples

  2. lineWidth = 66

  3. puts ''

  4. puts ''

  5. title = 'Sample Table'.center(lineWidth)

  6. row1 = 'LAST NAME'.ljust(lineWidth/3)

  7. row1 += 'FIRST NAME'.ljust(lineWidth/3)

  8. row1 += 'ID'.ljust(lineWidth/3)

  9. row2 = 'Hough'.ljust(lineWidth/3)

  10. row2 += 'Jessica'.ljust(lineWidth/3)

  11. row2 += '007'.ljust(lineWidth/3)

  12. puts title

  13. puts row1

  14. puts row2

Result: