soupybionics soupybionics - 6 months ago 21
Ruby Question

Where to learn ruby DSL?

Vagrant.configure(2) do |config|
config.vm.define "chefnode" do |chefnode|
chefnode.vm.box = "geerlingguy/ubuntu1604"
chefnode.vm.hostname = 'cnode'
chefnode.vm.network "public_network"
end

config.vm.define "chefserver" do |chefserver|
chefserver.vm.box = "geerlingguy/ubuntu1604"
chefserver.vm.hostname = 'cserver'
chefserver.vm.network "public_network"
end
end


I am badly struggling on the above (vagrant dsl) code. Is this Ruby DSL or plain Ruby or something else? Why is there an "=" sign for vm.box and vm.hostname, but not for vm.network??

Answer

first of all it's ruby, which as a language can be very handy when you want to create your own DSL. All DSLs are built using base ruby concepts like blocks (do ... end) - no magic here. In your case we may say it's DSL created by Vagrant owners.

Why do they once use = and an another time not? In this specific case I assume it's caused by fact you have one argument which is a String and is required, and later on yo may pass a hash with different setup options, like in example from documentation.

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080
end

If they wanted to use = they would have to enforce you to pass a Hash in which name key would be required and the rest would be optional, I mean something like this:

config.vm.network = { name: "default" }
Comments