ruby - Setting an instance variable from a block -


How do I get something like the following so that when I set the s variable in the block, it Set the @subject example variable in my subject class?

  Category topic def theme (& amp; blk) blk.call (@subject) if block_life is done? @ Block_given until search? End end my_topic = subject.nip my_topic.subject # = & gt; Zero my_topic.subject do S | S = ['one', 'two', 'three'] s.pop p s # = & gt; ['One', 'two'] end p my_topic.subject # = & gt; The way you want to be [a, 'two']  

You can not do that, block logic refers to the same object as the example instance, but they are completely different variables and one set will never be set to two, there are two options:

< Ol>
  • Set the variable in the result of the block, then it will be:

      class subject def subject = subject = if block_give is received? @ Block_given until search? End of End  

    and inside the block:

      my_topic.subject do s = ['one', 'two', 'three'] s Pop ps # = & gt; ['One', 'two'] end of  
  • topic method instance_eval block so block Can explicitly set the frequency variable


  • Comments

    Popular posts from this blog

    windows - Heroku throws SQLITE3 Read only exception -

    lex - Building a lexical Analyzer in Java -

    python - rename keys in a dictionary -