Using 'return' in a Ruby block

后端 未结 7 845
太阳男子
太阳男子 2020-11-28 03:54

I\'m trying to use Ruby 1.9.1 for an embedded scripting language, so that \"end-user\" code gets written in a Ruby block. One issue with this is that I\'d like the users to

7条回答
  •  悲哀的现实
    2020-11-28 04:15

    Where is thing invoked? Are you inside a class?

    You may consider using something like this:

    class MyThing
      def ret b
        @retval = b
      end
    
      def thing(*args, &block)
        implicit = block.call
        value = @retval || implicit
        puts "value=#{value}"
      end
    
      def example1
        thing do
          ret 5 * 6
          4
        end
      end
    
      def example2
        thing do
          5 * 6
        end
      end
    end
    

提交回复
热议问题