Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local variables that have the same name as an input #395

Closed
ngan opened this issue Nov 8, 2016 · 3 comments
Closed

Local variables that have the same name as an input #395

ngan opened this issue Nov 8, 2016 · 3 comments
Labels

Comments

@ngan
Copy link
Contributor

ngan commented Nov 8, 2016

I sometimes have something like this:

class CreateTransaction < ActiveInteraction::Base
  object :transaction

  def execute
    transaction = transaction.lock!
    # ...

   return transaction
  end
end

Note: I know that there exists a with_lock method that reloads the current instance of the transaction object, but this is a simplified example. In the real world scenario I sometimes need to lock multiple rows across multiple tables (with SQL joins) and in that case I have to overwrite the variable.

Anyways, back the point. When you assign a local variable shares the same as an input, things get really funking. I think it's because transaction= is define by AI::Base wherein it perform the coercion? Is there a (nice) workaround for this other than renaming the variable?

@tfausak
Copy link
Collaborator

tfausak commented Nov 8, 2016

Great question! I I don't think I've ever encountered this problem, so I don't have a solution off the top of my head. In general I try to think of the interaction inputs as immutable constants.

ActiveInteraction does define attr_accessor for each input. It doesn't do any coercion there, though. You should be able to freely re-assign values in your execute block. What exactly is the problem you're hitting?

@ngan
Copy link
Contributor Author

ngan commented Nov 9, 2016

I can't remember what happens exactly on re-assignment...but I think I understand (and like) your concept of:

In general I try to think of the interaction inputs as immutable constants.

@ngan ngan closed this as completed Nov 9, 2016
@ixio
Copy link

ixio commented Mar 6, 2019

If anyone else has this question, for example if you wanted to use ||= to set a default at runtime, the problem comes from https://docs.ruby-lang.org/en/2.5.0/syntax/assignment_rdoc.html#label-Local+Variables+and+Methods.

A simple way to fix this is to use the '@' sign.

class WontWork < ActiveInteraction::Base
  string :a, default: nil
  def execute
    a ||= 'default'
  end
end

class WillWork < ActiveInteraction::Base
  string :a, default: nil
  def execute
    @a ||= 'default'
  end
end

WontWork.run!            # "default"
WontWork.run!(a: 'test') # "default"
WillWork.run!            # "default"
WillWork.run!(a: 'test') # "test"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants