0

I have the following class with the following method(s):

class Ad
  def generate
    # Find logo
    @logo = create_logo(arg1,arg2...)
  end

  def create_logo(arg1,arg2,..)
    #Logic that sets @logo
    return @logo
  end
end

So, as you can see I am using an @instance_variable as return values and receiving values of methods. I believe that by doing so, methods are more testable and somehow it makes the code clear, but do you think this is correct in Ruby?

Or this would be more correct:

class Ad
  def generate
    # Find logo
    create_logo(arg1,arg2...)
  end

  def create_logo(arg1,arg2,..)
    #Logic that sets @logo
  end
end

Thanks

9
  • Where are you using instance variables as arguments? Commented Jan 15, 2014 at 17:42
  • Ok. My question would be more something like... Is it normal for a method to return an instance variable? Or should just be modified inside the method? Commented Jan 15, 2014 at 17:42
  • @Linuxios adapted the question. Commented Jan 15, 2014 at 17:43
  • This question appears to be off-topic because it is a request for code review Commented Jan 15, 2014 at 17:57
  • Rune FS - This is not a code review. I am trying to find out if this is a correct approach in Ruby programming. Commented Jan 15, 2014 at 18:00

2 Answers 2

1

Your first approach is much more similar to a functional programming approach, except I'd guess that it will have none of the benefits of function programming because it is doing file IO, side effects, etc.

Ask yourself this:

Is the return value of this method solely the result of its arguments, and it has no side effects? (setting instance vars, file IO, database, etc.)

If the answer is yes, than create_logo should be implemented like this:

def create_logo(arg1,arg2,..)
  logo = some_logic()
end

And called with a set to @logo. If not, this whole pretend almost function programming thing is useless.

The benefit of this type of implementation is that you can unit test it, it is a limited, small self contained part of your program, and it is simple. If the above question's answer is a no, it will do nothing for you.

Sign up to request clarification or add additional context in comments.

6 Comments

Sometimes I have problem explaining myself. I am so sorry. My question was more related as...Should the instance variables be set in the methods, or they should be set as returning values of the methods like @logo = create_logo - instead of create_logo setting @logo
So, if the instance variable is not returned but just set in the method, I guess is difficult to test what this method does to it, right?
@HommerSmith: Again, it depends on the work create_logo has to do to create the logo. If it is a method that relies completely on it's arguments, use the first. If it is more of an internal utility method for your class that has side effects, set it inside create_logo. Don't worry, I'm not being very clear today either.
@HommerSmith: Not really, because you can always run the method and just read the value of the instance variable afterwords.
But don't do both, which boils down to a redundant @logo = @logo right? I thought that was the original question.
|
1

In the first example, you're essentially saying @logo = @logo, which seems like a strange thing to do. I'd be assigning a value to @logoin just one place, either in the generate method (and just use a local variable in create_logo if you need to, or set @logo in create_logo as per your second example.

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.