LJZN

每天更新Rails练习项目到Github~

» Home
» Category
» About Me
» Github

40模块、类和对象

03 Jul 2016 » LRTHW
class Song

	def initialize(lyrics)
		@lyrics = lyrics
	end
	
	def sing_me_a_song()
		@lyrics.each {|line| puts line }
	end
	
	def add_lyrics()
		new_lyrics = $stdin.gets.chomp
		@lyrics.push(new_lyrics)
	end
end

hblyrics = ["Happy birthday to you", "I don't want to ger sued", "So I will stop right there"]

happy_bday = Song.new(hblyrics)

happy_bday.sing_me_a_song()

happy_bday.add_lyrics()

happy_bday.sing_me_a_song()

Related Posts