No operator needed for assignment in Ruby?

时间:2015-06-25 18:44:09

标签: ruby

I've been trying to learn Ruby, and I recently reoccuringly saw something like this in a few people's Github repo: Vagrant.configure(2) do |config| config.vm.box = "ubuntu/ubuntu-15.04-snappy-core-edge-amd64" config.vm.network "forwarded_port", guest:80, host:8080 end Why is there no assignment operator after config.vm.network? Is this non-standard Ruby? Or are they passing arguments in a function? What's going on here?

2 个答案:

答案 0 :(得分:3)

You're passing arguments in a function. See here.

答案 1 :(得分:2)

Your guess is correct. The config.vm object has a network method. The Ruby syntax is pretty relaxed, but this call can be written more "formally" like config.vm.network("forwarded_port", {guest: 80, host: 8080}) This relaxed form that you are seeing often is a common convention. (Hence why you are seeing it quite often!) Hope this helps.