Simple ruby code demonstrating default arguments is throwing name/method error -


seems pretty straightforward. trying write simple code has default argument. throws error name error , method error. code should able take argument , print arg , if not arg given print "meat". why throwing errors?

def meal_choice( meal = "meat")  puts "#{meal}" end  failures: 

1) #meal_choice should default meat failure/error: expect(meal_choice).to eq("meat")

 nameerror:    undefined local variable or method `meal_choice' #<rspec::examplegroups::mealchoice:0x007fb4ec159188>  # ./spec/meal_choice_spec.rb:3:in `block (2 levels) in <top (required)>' 

2) #meal_choice should allow set meal failure/error: expect(meal_choice("vegan")).to eq("vegan")

 nomethoderror:    undefined method `meal_choice' #<rspec::examplegroups::mealchoice:0x007fb4ec14b0d8>  # ./spec/meal_choice_spec.rb:7:in `block (2 levels) in <top (required)>' 

finished in 0.00125 seconds (files took 0.12294 seconds load) 2 examples, 2 failures

failed examples:

rspec ./spec/meal_choice_spec.rb:2 # #meal_choice should default meat rspec ./spec/meal_choice_spec.rb:6 # #meal_choice should allow set meal

it working code:

[1] pry(main)> def meal_choice( meal = "meat") [1] pry(main)*   puts "#{meal}"  [1] pry(main)* end   #=> :meal_choice [2] pry(main)> meal_choice #=> meat #=> nil [3] pry(main)> meal_choice(:sdgsdg) #=> sdgsdg #=> nil 

you must having typo in method call or wrongly calling method.


Comments