Question

i am trying to run following curl command from my ruby code

Kernel.system'curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"project\":{\"name\":\"BB\",\"description\":\"Book\"}}" "http://localhost:3000/company/projects?auth_token=mRFWyxfdPKHsDb4HhyLP"'

I want to do something like

name = "BB"(using a variable)

and then executing

Kernel.system'curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"project\":{\"name\":"#{name}",\"description\":\"Book\"}}" "http://localhost:3000/company/projects?auth_token=mRFWyxfdPKHsDb4HhyLP"'

but this approach is not working...its taking #{name} as string.

Please help me with this.Let me know if i am doing something wrong.

Thanks.

Was it helpful?

Solution

in order to enable string interpolation you will need to use double quotes in your string. The topic has been covered here: Double vs single quotes

also it would make your code better to read if you refactored it and assigned the parts to variables before joining them together into one string.

so:

host_name = 'stackoverflow.com'

Kernel.system 'curl -I #{host_name}' 

will not work whereas

Kernel.system "curl -I #{host_name}"

will

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top