0

I'm pretty newbie in ruby and I'm not sure that the question be suitable, but this is my problem.

I have a function with unlimited string parameters. I want to know if all of them are in the http request. This is my code :

def http__params_exists *list
    list.each do |p|
        if params[:'#{p}'].nil?
            return false
        end
    end
    true
end

Take an example : I have a query with the "lat" parameter. The condition that I want to be execute is if params[:lat].nil?

I tried some tricks, but all failed. Thanks.

ps :An other question about ruby. I don't know exactly the kind of objects that symbols like :method_not_allowed (for example) are. I know that we can call a function naming some parameters, but this object is already defined somewhere, as if it was a public variable of the controller, but it doesn't seems to be.

1 Answer 1

2

You need to use " instead of ' so that the variable is interpreted. Example

a = 'string'

:'#{a}'
==> :"\#{a}"

:"#{a}"
==> :string

:"#{a}" == :string
==> true

So, your code shall be

...
if params[:"#{p}"].nil?
  return false
end
...

This is an interesting link to learn more about Ruby symbols.

Sign up to request clarification or add additional context in comments.

1 Comment

p.to_sym would be another option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.