Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help writing birthday messages in Ruby

Tags:

ruby

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.

like image 712
user3188399 Avatar asked Nov 30 '22 19:11

user3188399


2 Answers

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:

card for Kyle in Sublime Text

like image 189
mechanicalfish Avatar answered Dec 22 '22 14:12

mechanicalfish


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

like image 36
sunnyrjuneja Avatar answered Dec 22 '22 14:12

sunnyrjuneja