2 Replies - 795 Views - Last Post: 04 March 2014 - 12:42 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

splat operator

Posted 03 March 2014 - 09:29 PM

Why isn't my splat operator on line 14 happily accepting the 3 args on line 60?

playl.rb:60:in `initialize': wrong number of arguments (3 for 0) (ArgumentError)
	from playl.rb:60:in `new'
	from playl.rb:60:in `<main>'



class Song
	attr_accessor :title, :artist

	def initialize(title, artist) 
		@title = title
		@artist = artist
		playing = false
	end
end

class Playlist
	attr_accessor :songs

	def intialize(*songs)
    @songs = songs
	end

	def add(*song)
		songs.push(song)
	end

	def num_of_tracks
		songs.length 
	end

	def play
		playing  = true
	end

	def play_all
		songs.each { |song| song.play  }
	end

	def stop
	    playing = false
	end

	def display
		puts songs
	end

	def remove(title)
		songs.each { |song|
			if title.capitalize == song.title.capitalize
				songs.delete(song)
			else
				nil
			end
		}
	end
end


#### Driver Code#### 
 
one_by_one = Song.new("One by One", "Sirenia")
world_so_cold = Song.new("World So Cold", "Three Days Grace") 
going_under = Song.new("Going Under", "Evanescence")
 
my_playlist = Playlist.new(one_by_one, world_so_cold, going_under)
 
lying_from_you = Song.new("Lying From You", "Linkin Park")
angels = Song.new("Angels", "Within Temptation")
 
my_playlist.add(lying_from_you, angels)
p my_playlist.num_of_tracks == 5
going_under.play
my_playlist.remove(angels)
p my_playlist.includes?(lying_from_you) == true
my_playlist.play_all
my_playlist.display


This post has been edited by heaphyg: 03 March 2014 - 09:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: splat operator

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,233
  • Joined: 21-June 11

Re: splat operator

Posted 04 March 2014 - 05:34 AM

View Postheaphyg, on 04 March 2014 - 05:29 AM, said:

Why isn't my splat operator on line 14 happily accepting the 3 args on line 60?


It is. However you misspelled the method name. So intialize does indeed accept arbitrarily many arguments, but initialize does not.
Was This Post Helpful? 2
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: splat operator

Posted 04 March 2014 - 12:42 PM

ugh! thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1