#[Ruby] I'm completely stumped when it comes to defining methods
68 messages · Page 1 of 1 (latest)
Increase your chance of getting help and look like a pro by sharing codeblocks not images. For example, you can type the following. Note, the ``` must be on their own line.
```
for number in range(10):
total += number;
```
Discord will render that as so:
for number in range(10):
total += number;
Click here to learn more about codeblocks: https://exercism.org/docs/community/being-a-good-community-member/writing-support-requests
From your image, you can see the definition of preparation_time_in_minutes expects a variable layers . The error is indicating when you called to preparation_time_in_minutes you forgot to provide a value for the variable
wrong number of arguments is saying "I expect something here, please tell me what it should be" on line 19
Why is line 19 lasagna.actual_minutes_in_oven(30) returning undefined method?
Discord should let you copy paste all that code into a codeblock!
It makes life easier for everyone. And is accessibility friendly.
The def total_time_in_minutes should not specify it's own values, so you can remove the = you were adding.
If you do hit the message limit, Discord lets you split it up or upload a code snippet.
def total_time_in_minutes(number_of_layers, actual_minutes_in_oven)
the exercise is learning to pass values from variables into the functions you have defined
I am just checking the default Ruby example. Sorry I made an error. The original definition of total_time_in_minutes should be used.
can you show the top lines of your code? Did you set the Lasagna::EXPECTED_MINUTES_IN_OVEN value?
EXPECTED_MINUTES_IN_OVEN = 40
def remaining_minutes_in_oven(actual_minutes_in_oven)
return EXPECTED_MINUTES_IN_OVEN - actual_minutes_in_oven
end```
that looks perfect
so it's just the remaining two definitions to clarify
so for the preparation_time_in_minutes you want to calculate layers
the documenation says assuming each layer takes you 2 minutes to prepare.
so layer * 2
return preparation_time_in_minutes * layers
end
def total_time_in_minutes(number_of_layers, actual_minutes_in_oven)
return (preparation_time_in_minutes * number_of_layers) + actual_minutes_in_oven
end
end
lasagna = Lasagna.new
lasagna.actual_minutes_in_oven(30)
lasagna.preparation_time_in_minutes(2)
lasagna.total_time_in_minutes(number_of_layers = 3, actual_minutes_in_oven = 20)```
you don't want to return preparation_time_in_minutes from itself
I initially had layers * 2 but I kept getting an error so I changed it
[Ruby] I'm completely stumped when it comes to defining methods
so the values in ( ) are the values sent to each def and those are the names you want to use
and with updating preparation_time_in_minutes to expect (layers) to recieve a number
when you define total_time_in_minutes your call to run preparation_time_in_minutes should provide the () value
so that (layers) has a number to calculate from
preparation_time_in_minutes(number_of_layers)
where (number_of_layers) is the first position argument in the def
you don't need the other lines outside of the class
you can run the test by running ruby lasagna_test.rb will provide the values for each function
to pass or fail
so the extra lines you defined at the bottom you should remove to make it clearer
You should remove these lines:
lasagna = Lasagna.new
lasagna.actual_minutes_in_oven(30)
lasagna.preparation_time_in_minutes(2)
lasagna.total_time_in_minutes(number_of_layers = 3, actual_minutes_in_oven = 20)
If it helps, could run through one of the test values
return (number_of_layers * preparation_time_in_minutes) + actual_minutes_in_oven
#The number of time per layer is 2 and there are 3 layers
end```
What did I do wrong here?
Is there an error message? A failed test?
It's much easier to debug "what's wrong" when there's an error to look at 😄
return (number_of_layers * preparation_time_in_minutes) + actual_minutes_in_oven
#return (3 * preparation_time_in_minutes) + actual_minutes_in_oven
#return 3 + actual_minutes_in_oven
#The number of time per layer is 2 and there are 3 layers
end``` I'm getting an error message ```1) Error:
LasagnaTest#test_total_time_in_minutes_for_multiple_layer:
ArgumentError: wrong number of arguments (given 1, expected 2)
/Users/alana/Exercism/ruby/lasagna/lasagna.rb:18:in `total_time_in_minutes'
lasagna_test.rb:31:in `test_total_time_in_minutes_for_multiple_layer'
2) Error:
LasagnaTest#test_total_time_in_minutes_for_one_layer:
ArgumentError: wrong number of arguments (given 1, expected 2)
/Users/alana/Exercism/ruby/lasagna/lasagna.rb:18:in `total_time_in_minutes'
lasagna_test.rb:24:in `test_total_time_in_minutes_for_one_layer'
6 runs, 4 assertions, 0 failures, 2 errors, 0 skips```
Looking at these two lines, can you understand what it's trying to tell you?
ArgumentError: wrong number of arguments (given 1, expected 2)
/Users/alana/Exercism/ruby/lasagna/lasagna.rb:18:in `total_time_in_minutes'
Erm. Rather, look at the instructions:
Define the Lasagna#total_time_in_minutes method that takes two named parameters
This article may help explain how the calls to each def work, so you can follow what the input values are doing
https://ruby-for-beginners.rubymonstas.org/writing_methods/usage.html
On each def line, you specify parameters inside the () eg (some_parameter_name)
When you need to make a call to send a value to the def line, you send argument values in the format of some_def_name(some_parameter_name)
In the exercise you have def preparation_time_in_minutes(layers) - it expects to receive a value for layers from somewhere - how do you send it this value?
You write code make a call with an argument value, in the format of preparation_time_in_minutes(some_parameter_name) where you replace some_parameter_name with a value you have inside total_time_in_minutes - and total_time_in_minutes has two values number_of_layers and actual_minutes_in_oven
The hint is in the number_of_layers and layers names.
Ruby for Beginners
class Lasagna
EXPECTED_MINUTES_IN_OVEN = 40
def remaining_minutes_in_oven(actual_minutes_in_oven)
EXPECTED_MINUTES_IN_OVEN - actual_minutes_in_oven
end
def preparation_time_in_minutes(layers)
layers * 2
end
def total_time_in_minutes(number_of_layers, actual_minutes_in_oven)
return (number_of_layers * preparation_time_in_minutes) + actual_minutes_in_oven
# change the line above so preparation_time_in_minutes is sending a number of 'layers' somewhere else in the code
end
end
I figured out what I was doing wrong and passed the test
Now I'm having difficulties with the log_line_parser exercise.
attr_accessor :message, :messagewarning, :log, :reformat
def initialize(message, messagewarning, log, reformat)
@message = message
@messagewarning = messagewarning
@log = log
@reformat = reformat
end
def message
# raise 'Please implement the LogLineParser#message method'
pp "#{@message}"
pp "#{@messagewarning}"
end
def log_level
#raise 'Please implement the LogLineParser#log_level method'
pp "#{@log}"
end
def reformat
#raise 'Please implement the LogLineParser#reformat method'
pp "#{@reformat}"
end
end
log_line = LogLineParser.new('[ERROR]: Invalid operation', "[WARNING]: Disk almost full\r\n",'[ERROR]: Invalid operation', '[INFO]: Operation completed')
pp log_line``` This is my code
and I received 11 error messages 11) Error: LogLineParserTest#test_error_message: ArgumentError: wrong number of arguments (given 1, expected 4) /Users/alana/Exercism/ruby/log-line-parser/log_line_parser.rb:4:in `initialize' log_line_parser_test.rb:8:in `new' log_line_parser_test.rb:8:in `test_error_message'
Do you understand the concepts that this exercise is giving you?
there's some reading here that helps to try and explain what this is attempting to do
https://ruby-for-beginners.rubymonstas.org/writing_classes.html
Particularly these two pages
https://ruby-for-beginners.rubymonstas.org/writing_classes/initializers.html
https://ruby-for-beginners.rubymonstas.org/writing_classes/instance_variables.html
Ruby for Beginners
Ruby for Beginners
Ruby for Beginners
so you understand where the values are appearing in each part of the code, helps you understand what code needs to change
LogLineParser.new is a call to run def initialize(message, messagewarning, log, reformat) inside class LogLineParser
the @ indicates a special type of variable
There is also a HINTS.md file with the exercise that has some other links that will help with the exercise
OK, this specific error is because you are adding lines that you don't need, rather than using the test file.
Each exercise has a _test.rb file to run to send input for testing. Please use those by running ruby <exercise name>_test.rb
If you remove your changes, and run the log_line_parser_test.rb file you will get the expected test output
11 runs, 0 assertions, 0 failures, 11 errors, 0 skips
You should only need to edit lines inside those provided by the exercise inside class LogLineParser ... end
that will help minimise confusion
this exercise is about handling on string values, so I think you might need to go back a few steps
def message should take the value of @line and return the string minus the "level" information eg [INFO]: Operation completed should become Operation completed
def log_level should take the value of @line and return the string in lower case minus the "message" information eg [INFO]: Operation completed should become info
def reformat should use the above to provide the expected output eg Operation completed (info)
def message
# raise 'Please implement the LogLineParser#message method'
message = @line # try different string operations on `@line` here to update the value of `message` that is returned
end
The README for the exercise gives examples of using a string array and string methods like .downcase and .strip