Classes are serious business!


can anybody please elaborate what's happening in the code ?


Please post the code so that I may be able to assist you better.

Please note these forums are separate from the code area, so please transfer the code here manually.

Thank you!

def create_record(attributes, raise_error = false)
  record = build_record(attributes)
  yield(record) if block_given?
  saved =
  raise if !saved && raise_error


Here's a line-by-line analysis of what's happening in your code:

  1. def create_record(attributes, raise_error = false): You are creating a new method/function called create_record which takes in two arguments: attributes and raise_error where raise_error has a default value of false.
  2. record = build_record(attributes): You are defining a variable record to be equal to the result returned by the method/function build_record which takes in an argument attributes.
  3. yield(record) if block_given?: You are calling a method yield which takes an argument record. Depending on the result of that which is true or false indicated by the ? in it, you are entering the conditional and testing a method block_given? which takes no arguments.
  4. saved = You are setting a variable called saved to the value of, which, presumably, is a method of the record object.
  5. set_new_record(record): You are calling a method/function set_new_record which takes in an argument record.
  6. record: You are returning the variable record.
  7. end: You are notifying Ruby of the end of the method definition.

Was this sufficient, or do you need more? Please let me know.


I think this much will do !
thanks mate .



Thanks for the detailed explanation. But how about this line?:

  • raise if !saved && raise_error

could you please explain it


Apologies, I hadn't seen that line previously.

That time means that if !saved && raise_error is a true expression, then to raise an error of type RecordInvalid for the record object.

Presumably saved and raise_error are previously defined methods or functions that help determine whether or not the need to raise an error is valid.


thks for your quick reply!

However, way too advanced for me, many stuff there that I have not seen before... I guess I got to be patient and keep going.

Cheers :smile:


Don't worry, with practice it will make sense.