Back to Setup Page

Valentine Generator

Now that we have an understanding of OOP and have used some Ruby syntax to write classes and create objects from them, we are ready to write a program that generates Valentine messages!


What are some attributes you would have in a valentines message? The one we are starting with is recipient.

Customize your Valentine

Use this as a starter kit. It has the code for a Valentine class, and what we call a method. It currently has only one attribute and is creating one instance.

  • Add at least 2 more attributes to your class.
  • Create several more objects.
  • Run your file to make sure you can see your Valentine messages!


In Ruby (and other proramming languages), we have ways to store a collection of data. One way is by using what we call an array. We can think of it like a list!

Here’s an example of an array of numbers:

numbers = [1, 3, 5, 7, 9]
p numbers

If you’d like to print out all your Valentines with one line of code, one way to do that is by storing them in an array. A couple changes were made from our original code in order to write this solution.

class Valentine
  def initialize(recipient)
    @recipient = recipient

  def create_valentine
    "To #{@recipient}: I love you!"

mom ="Mom")
bff ="BFF")

valentines = [

puts valentines

In OOP, it is common to store instances in an list, or array, like that.

Wrap Up