#[Ruby] I'm completely stumped when it comes to defining methods

68 messages · Page 1 of 1 (latest)

heavy perch
#

What language? Can you share code as text in a codeblock ```? Images are hard to copy text from. And even harder for the visually impaired.

sacred isleBOT
vernal portal
#

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

spare trench
heavy perch
#

Discord should let you copy paste all that code into a codeblock!

#

It makes life easier for everyone. And is accessibility friendly.

vernal portal
#

The def total_time_in_minutes should not specify it's own values, so you can remove the = you were adding.

heavy perch
#

If you do hit the message limit, Discord lets you split it up or upload a code snippet.

vernal portal
#

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.

vernal portal
spare trench
#
  EXPECTED_MINUTES_IN_OVEN = 40

 
  def remaining_minutes_in_oven(actual_minutes_in_oven)
    return EXPECTED_MINUTES_IN_OVEN - actual_minutes_in_oven
  end```
vernal portal
#

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

spare trench
#
    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)```
vernal portal
#

you don't want to return preparation_time_in_minutes from itself

spare trench
#

I initially had layers * 2 but I kept getting an error so I changed it

vernal portal
#

sorry it should match the variable name

#

(layers)

#

layers * 2

heavy perch
#

[Ruby] I'm completely stumped when it comes to defining methods

vernal portal
#

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

spare trench
#
    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```
spare trench
#

What did I do wrong here?

heavy perch
#

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 😄

spare trench
# vernal portal You should remove these lines: ``` lasagna = Lasagna.new lasagna.actual_minutes_...
    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```
heavy perch
#

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

vernal portal
#

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.

#
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
spare trench
#

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'

vernal portal
#

Do you understand the concepts that this exercise is giving you?

#
#

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

vernal portal
#

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)

vernal portal
#
  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