Add Two Struct Objects#14
Add Two Struct Objects#14robskrob wants to merge 4 commits intoRubyoffRails:masterfrom robskrob:master
Conversation
train.rb
Outdated
There was a problem hiding this comment.
what does this output? Is it what you would expect?
There was a problem hiding this comment.
Everything works except: puts "Engines: #{train.number_of_engines}"
There was a problem hiding this comment.
The output is the following:
$ ruby train.rb
Destination: [#<struct Passenger name={:name=>"Bill Smith"}>, #<struct Passenger name={:name=>"Sarah Robot"}>]
Cars: {:destination=>"New York", :number_of_engines=>4, :caboose=>true, :number_of_cars=>5}
Caboose:
Passengers: [#<struct Passenger name={:name=>"Bill Smith"}>, #<struct Passenger name={:name=>"Sarah Robot"}>]
train.rb:11:in <main>': undefined methodnumber_of_engines' for #Train:0x007fb9311a86b8 (NoMethodError)
There was a problem hiding this comment.
So, the destination seems wrong, as well as Cars, Caboose, and Passengers.
If your definition for train is Struct.new(:destination, :number_of_cars, :caboose, :number_of_engine) then what are you sending in?
Train = Struct.new(:destination, :number_of_cars, :caboose, :number_of_engine)There isn't a place for passengers.
And to simplify things, let's change
train = Train.new( passengers, {destination: 'New York', number_of_engines: 4, caboose: true, number_of_cars: 5})to
train = Train.new( passengers, 'New York', 4, true, 5)There was a problem hiding this comment.
perhaps I should watch the video again...it is not clear to me what should
be a hash vs a Struct and how to construct it the way you want it. I will
start over again.
On Tue, Jan 14, 2014 at 12:46 PM, Jesse Wolgamott
[email protected]:
In train.rb:
@@ -0,0 +1,40 @@
+Passenger = Struct.new(:name)
+Train = Struct.new(:destination, :number_of_cars, :caboose, :number_of_engine)
+
+passengers = [Passenger.new(name: 'Bill Smith'), Passenger.new(name: 'Sarah Robot')]
+train = Train.new( passengers, {destination: 'New York', number_of_engines: 4, caboose: true, number_of_cars: 5})
+
+puts "Destination: #{train.destination}"
+puts "Cars: #{train.number_of_cars}"
+puts "Caboose: #{train.caboose}"
+puts "Passengers: #{passengers}"So, the destination seems wrong, as well as Cars, Caboose, and Passengers.
If your definition for train is Struct.new(:destination, :number_of_cars,
:caboose, :number_of_engine) then what are you sending in?Train = Struct.new(:destination, :number_of_cars, :caboose, :number_of_engine)
There isn't a place for passengers.
And to simplify things, let's change
train = Train.new( passengers, {destination: 'New York', number_of_engines: 4, caboose: true, number_of_cars: 5})
to
train = Train.new( passengers, 'New York', 4, true, 5)
—
Reply to this email directly or view it on GitHubhttps://github.com//pull/14/files#r8868170
.
Robert Jewell
1.914.400.5219
|
Here's my second attempt |
There was a problem hiding this comment.
Hey Robert --
I don't want you to use the key,value interface of Struct. I'm trying to get you to use the dot syntax, as an object, from a Struct. It's a way to easily pass around an object -- the receiver that you pass to, won't know it's a struct.
I'm not too sure if I understood the instructions correctly or where you were trying to go with:
passengers = [Passenger.new(name: 'Bill Smith'), Passenger.new(name: 'Sarah Robot')]
Train.new( passengers, {destination: 'New York', number_of_engines: 4, caboose: true, number_of_cars: 5})