0

Ruby Proc クラスを勉強しています。「def state="メソッドが実行される理由がわかりません。

また、「t1.state = 1」が「def state=(1)」になる理由も知りたいです。

"def state" と "def state=" の違いがわかりませんか?

この接続 "&proc>proc>proc.call" は理解できます。

    # -*- coding: utf-8 -*-
    class Terminal
      def initialize
        @connected = []
        @current_state = nil
      end
      def connect(&proc)
        @connected << proc
      end
      def current_input_terminal(terminal)
        connect do |hoge|
          terminal.state = hoge
        end
      end
      def state=(current)
        if current != 0 && current != 1
          raise(ArgumentError, "input 0 or 1")
        end
        if @current_state != current
          @current_state = current
          @connected.each do |i|
            i.call(current)
          end
        end
      end
      def state
        @current_state
      end
    end

    t1 = t2 = Terminal.new()
    t1.current_input_terminal(t2)
    t1.state = 1
    puts(t2.state,t1.state)
4

1 に答える 1

0

この式:

Terminal.new.state = 1

メソッドを呼び出しますdef state=(current)

そしてこの表情

puts Terminal.new.state 

メソッドを呼び出すdef stateか、データを読み取ります

于 2012-03-07T18:24:15.673 に答える