0

I'm just wondering if there is any more beautiful method to create object in the method and return it ?

Here is my code:

class Example
   def Example.load_from_file(filename)
     example = Example.new
     # some code
     example
   end
end

May be there is some Ruby idiom which allow me not to write example at the end of the method. Can you refactore this code or it is goog enough now ?

1 Answer 1

1

Assuming that you have example that is mutable and you keep applying destructive methods to it within "some code":

class Example
  def Example.load_from_file(filename)
    Example.new.tap do |example|
      # some code
    end
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.