Meow
Meow

Reputation: 1742

No operator needed for assignment in 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?

Upvotes: 1

Views: 48

Answers (2)

dave_slash_null
dave_slash_null

Reputation: 1124

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.

Upvotes: 2

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 18567

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

Upvotes: 3

Related Questions