So my boyfriend's 20th birthday is coming up soon and he's a programmer, so I thought I would make him a cake with ruby code that says "happy 20th birthday, Kyle" . I would also like to give him a card that says "happy birthday to a special nerd", again, in ruby code.
Thanks for taking the time to read this. It means a lot to me.
For a cake I would go with kopischke's answer, but for a card where there is more place I would choose to do this:
class Card
attr_accessor :event, :person
def initialize(event, person)
@event = event
@person = person
end
def message
"Happy #{event} to a #{person}!"
end
end
card = Card.new('birthday', 'special nerd')
puts card.message
Here's how that looks like in an editor for a nicer syntax highlighting:
I like mechanlicalfish's answer for a card but for a cake I would suggest:
@my_special_nerd = "Kyle"
puts "Happy Birthday #{@my_special_nerd}"
Just so you know, @my_special_nerd = "Kyle"
assigns a variable called special nerd with the String Kyle. Kinda of like x =
that you learned in Algebra but you can put sentences on the right side.
puts
tells the program to print the program to standard output which is usually the programmer's screen.
"Happy Birthday ..."
is a string. Strings are how we store sentences that programmers can read.
"Happy Birthday #{@my_special_nerd}"
so #{@my_special_nerd}
tells Ruby to replace the variable @my_special_nerd
with the value Kyle.
If there program was run using Ruby, it would print to the programmer's screen:
Happy Birthday Kyle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With