-
Notifications
You must be signed in to change notification settings - Fork 18
Add Two Struct Objects #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
train.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this output? Is it what you would expect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything works except: puts "Engines: #{train.number_of_engines}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
notifications@github.comwrote:
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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})