0

Can someone give me the best possible solution for this,

I have a class

Class A

  def a
    return "a" if @logic
    return "b"
  end

  def change_logic
    @logic = true
  end

end

In my solution, I should be able to do

object = A.new
object.change_logic
object.a #print "a"

or

object = A.new
object.a #print "b"

To implement this I created the method change_logic & it will change @logic instance variable.

But I need to implement this without having a instance variable like @logic

What will be the best way to do it??

7
  • 3
    question - why would you want to do it without having a instance variable like @logic? Commented Sep 7, 2018 at 12:17
  • 2
    A variable is exactly the right solution for this. Why don't you want to use one? Also, what precisely do you mean by "without having a instance variable like @logic"? Is it enough to just rename the instance variable? Would a closure closing over a local variable count as "like an instance variable"? Would a class variable with a map from instances to values count as "like an instance variable"? Can you give a precise, unambiguous, objective, exact, complete specification of what exactly "like an instance variable" means? Commented Sep 7, 2018 at 12:17
  • 1
    Why can't you use an instance variable? do you still want to call a change_logic method to modify a return value? I can't imagine the reason behind that requirement Commented Sep 7, 2018 at 12:18
  • I would like to have a different approach rather than having a boolean type instance variable & a method to change that variable. May be delegates or attr_writer?? Commented Sep 7, 2018 at 12:24
  • 1
    attr_writer is just a shorthand way of creating an instance variable and accesssor method and delegates is just exposing an interface of another object. Either way you end up with an instance variable and some mechanism for setting it. Commented Sep 7, 2018 at 12:27

1 Answer 1

1

If you need to change what's returned conditionally but you don't want the condition at instance level then the method needs to be informed of what that condition is. For example,

Class A
  def a(logic = false)
    logic ? 'a' : 'b'
  end    
end

then it would be invoked instead as:

object = A.new
object.a(true) #print "a"

or

object = A.new
object.a #print "b"
Sign up to request clarification or add additional context in comments.

4 Comments

@yzalavin not sure I actually agree with that ... according to that documentation it states 'Make the decision what method to call in the initial caller of hit_the_switch' ... ie you are simply moving the if else block outside the method to somewhere else ... just exactly why does it reek to let the method itself decide what to do rather than the caller?
"moving the if else block outside the method to somewhere else" – you could also say "to where it belongs". The method (a) doesn't decide what to do, the caller does by passing true or false. The caller could therefore just as well invoke different methods (e.g. object.b instead of object.a). That would simplify A and probably make the code easier to follow.
I really think it depends on the context to be honest ... and I agree the contrived solution isn't the best but neither was the question ... it was unclear just exactly where or how the condition would be determined. As it turns out using an instance variable is fine but what the OP actually wanted was to make his code look cleaner. I'm interested how would you suggest removing the code smell now that the condition is determined from an instance variable that may be set outside the scope of the calling method?

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.