开发者

Unexpected '\n' in Ruby Fiber

开发者 https://www.devze.com 2023-01-06 22:45 出处:网络
I\'m trying to wrap my head around multithreading, so I\'m playing around with Fibers in Ruby. However, when I try to run my script, it tells me I have an unexpected newline character after my ter开发

I'm trying to wrap my head around multithreading, so I'm playing around with Fibers in Ruby. However, when I try to run my script, it tells me I have an unexpected newline character after my ter开发者_开发知识库nary statement. Did I miss something about the syntax, here?

timer = Fiber.new do |power|
  power = power.nil? ? 'on' | power 
  start = Time.now 
  loop do 
    if power == 'off'
      now = Time.now
      puts now - start
    end
    power = Fiber.yield
  end
end


power = power.nil? ? 'on' | power

The proper syntax for this is power = power.nil? ? 'on' : power, with a colon instead of a pipe.

However you could just write this power = 'on' if power.nil?, which is a bit shorter and probably more readable for most people.

Also as a sidenote: is there a paticular reason that you're using 'on' and 'off' instead of true and false?

0

精彩评论

暂无评论...
验证码 换一张
取 消