Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need save {"individual"=>"true", "multi-user"=>"true"} to a string and then parse it to a JSON representation of the same hash. Unfortunately, this produces the following error:
JSON::ParserError in WizardsController#step
795: unexpected token at '{"individual"=>"true", "multi-user"=>"true"}'
Use to_json on the hash, before you attempt to manipulate it:
{"individual"=>"true", "multi-user"=>"true"}.to_json
=> "{\"individual\":\"true\",\"multi-user\":\"true\"}"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
My rails app is receiving the following from the json. How do I access the name key of attributes and the calendar id in Ruby.
Parameters: {"data"=>{"type"=>"user-events", "attributes"=>{"name"=>"An event", "location"=>"University of Illinois at Urbna-Champaign", "notes"=>"Testing my eventf\n", "all-day"=>false, "recurring"=>false, "sunday"=>false, "monday"=>false, "tuesday"=>false, "wednesday"=>false, "thursday"=>false, "friday"=>false, "saturday"=>false, "start-date"=>"01-16-2018", "stop-date"=>"01-16-2018", "start-time"=>"04:32PM", "stop-time"=>"05:32PM"}, "relationships"=>{"calendar"=>{"data"=>{"type"=>"calendars", "id"=>"685"}}}}}
Do I use the code below to access the name attribute ?
params[:data][:attributes][:name]
Also, do I use the following to access the id for the calendars ?
params[:data][:relationships][:calendar][:data][:id]
Thanks in advance for your help
If your Hash name is Parameters
Parameters['data']['attributes']['name'] # "An event"
Parameters['data']['relationships']['calendar']['data']['id'] # "685"
if it's params, then
params['data']['attributes']['name'] # "An event"
params['data']['relationships']['calendar']['data']['id'] # "685"
will access the attributes.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Is it possible to have JSON.parse work 2 layers deep so that a hash within a hash will get parsed as well? Is there a method for or do I have to do something like JSON.parse(JSON.parse(...)['foo'])?
JSON.parse doesn't care about your hash structure:
> str = JSON.dump({foo: {bar: {baz: :qux}}})
=> "{\"foo\":{\"bar\":{\"baz\":\"qux\"}}}"
> p = JSON.parse(str).with_indifferent_access
=> {"foo"=>{"bar"=>{"baz"=>"qux"}}}
> p[:foo][:bar][:baz]
=> "qux"
(Well, it cares if you have a malformed string, but that's something else altogether.)
So, what are you asking?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My user has the attribute:
:step1_local
:step2_local
:step3_local
...
...
:local1
:local2
:local3
I would like to change an attribute value based on another set of attributes on the same model. I would like to do some processing mapping on user, say:
def magic (user)
user.local(1..3) = process(user.step(1..3)_local)
end
The code above of course does not work (example). I am not sure how to do it dynamically without going through each attributes individually. I want to map processing one to another. Any ideas?
You can use Object#public_send and method, like this:
def magic(user)
(1..3).each do |n|
user.public_send("local#{n}=", process(user.read_attribute("step#{n}_local")))
end
end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does someone know why
hash['City'] = {}
hash['City']['answer0'] = 'foo'
Get the following error:
undefined method `+#' for {"answer0"=>"foo"}:Hash
Thanks
If you want to have 'multi-dimentional' hashes you need to properly define the hash like so:
a = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
Then you can do:
a['city']['answer0'] = 'foo'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know there some object has nil? method, I want to know is there some method with nil?
There is a similar answered post here: a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
Summarizing:
If you are using rails (not just ruby) there are three methods in your toolkit: .nil?, .empty? and .blank?
Follow the link to see the good description
w.nil?
inverse of
!w.nil?
You could use present?, which acts as a sort of !blank?
you could use defined? as well
No.
1.9.3p125 :002 > Object.methods.grep(/nil/)
=> [:nil?]