Get value of a key in hash of hash [closed] - ruby-on-rails

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 have a hash e.g.
{{"badan"=>1, "bau"=>1, "China,"=>1, "pilot."=>1, "RT"=>1, "penentu"=>1, "merupakan"=>1, "China, test test bau"=>1, "satu"=>1, "merupakan salah salah satu"=>1, "RT #WOWFakta: #WOWFakta: Di"=>1, "Di"=>1, "seorang pilot."=>1, "kelulusan menjadi menjadi seorang"=>1, "seorang"=>1, "salah"=>1, "#WOWFakta:"=>1, "satu penentu penentu kelulusan"=>1, "Di Beijing Beijing China,"=>1, "menjadi"=>1, "kelulusan"=>1, "test"=>1, "bau badan badan merupakan"=>1, "Beijing"=>1}=>{"kebali"=>1, "kff"=>1, "box"=>1, "#zannahoctavia"=>1, "kebali kamu kamu ca?"=>1, "ca?"=>1, "kampus"=>1, "kantin"=>1, "kff hah hah goods"=>1, "#zannahoctavia haha haha kali"=>1, "aja"=>1, "box kemang kemang aja"=>1, "yaa"=>1, "china"=>1, "hah"=>1, "hah. Jadi Jadi kebali"=>1, "goods pw pw tapi"=>1, "aja di di kff"=>1, "di"=>1, "pw"=>1, "kemang"=>1, "haha"=>1, "kali kantin kantin kampus"=>1, "Jadi"=>1, "tapi"=>1, "tapi yaa yaa hah."=>1, "kampus china china box"=>1, "hah."=>1, "kamu"=>1, "kali"=>1}}
and I want to retrieve all keys' values in a very efficient way. How can I do this?

You can iterate your hash like that it contain hash within hash
h.each do |key, value|
puts key
value.values.each do |v|
puts v
end
end

your hash contains a key and a value that is also a hash. You can try the following
hash.flatten.map(&:keys).inject(:+)
or use flat_map
hash.flatten.flat_map(&:keys)

Related

Lua: Getting passed args when called function [closed]

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 2 years ago.
Improve this question
I want to get the parameters that passed to a specific function.
for example:
load("return 2+1")()
wanted output:
return 2+1
I have no idea after reading debug library :(
If I correctly understand what you want, override the load function prior to calling it:
local global_load = load
local function load (...)
print (...) -- or use whatever debug tool to see the arguments.
return global_load (...)
end
You can redefine any function this way:
local function verbose (func)
return function (...)
print (...) -- or use whatever debug tool to see the arguments.
return func (...)
end
end
local load = verbose (load)
print (load 'return 2 + 1' ())

create json object from string with ruby [closed]

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 am really new about Ruby development. I am trying to create a json file with strings. My json file like below. Can you help to me
{
"App":{
"properties":{"color":"red"},
"screens":[
{"id":"page1", "properties":{"color":"red"}, "elements":[
{"type":"txtbox", "properties":{"color":"red"}},
{"type":"button", "properties":{"color":"red"}}
]
},
{"id":"page2", "properties":{"color":"red"}, "elements":[
{"type":"txtbox", "properties":{"color":"red"}},
{"type":"button", "properties":{"color":"red"}}
]
}
]
}
}
You can parse JSON with ruby from a hash:
require 'json'
my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"
Or generate it from a hash:
require 'json'
my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
Have a look at the JSON documentation.

Json and Ruby creating a string [closed]

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 have a method that I can to push to a JSON call. The function looks like:
def my_function(name, text , id_person, id_company)
end
What I want is to get:
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
Is there any easy way to do this?
The question is a bit unclear, but let me try with few answer possibilities
First of all this is not a JSON format
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
There should not be double quote (") before semicolon (:)
obj = {
my_function:
{
name: "names_value",
text: "text_value",
id_person: "id_person_value",
id_company: "id_company_value"
}
}
If you want to get each of the data in that "JSON" format, you could do this
name = obj['my_function']['name'] #name_value
text = obj['my_function']['text'] #text_value
and so on...
But if you want to make a string data into JSON format, you could do this
def my_function(name_value, text_value, id_person_value, id_company_value)
{
my_function:
{
name: name_value,
text: text_value,
id_person: id_person_value,
id_company: id_company_value
}
}.to_json
end
I hope it's answering your question...

How to get hours from array of datetime attributes? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
=> [#<WelcomeCall id: 16, call_at: "2013-11-06 12:00:00">,
#<WelcomeCall id: 17, call_at: "2013-11-06 13:00:00">,
#<WelcomeCall id: 18, call_at: "2013-11-06 17:00:00">,
#<WelcomeCall id: 19, call_at: "2013-11-06 14:00:00">]
I would like to get an array of hours form all these WelcomeCall objects.
=> [12, 13, 17, 14]
WelcomeCall is rails model.
I used for that:
def self.scheduled_hours date
by_date(date).map do |wc|
wc.call_at.hour
end
end
but maybe there is better way?
You want to use map.
Try something like this:
new_array = old_array.map{ |welcome_call| welcome_call.call_at.strftime('%H').to_i }
Assuming that your welcome_call.call_at is a Date. If it's not a Date object, then try:
new_array = old_array.map do |welcome_call|
date = Date.parse(welcome_call.call_at)
date.strftime('%H').to_i
end
You could iterate over the array and parse the call_at datetime object using strftime('%H').to_i.
welcome_calls.map {|w| w.hour}
Parsing the Date to a String and then reparsing the String to a Fixnum is not as fast I think.
I just tried something and it worked, i'm not sure if it's the right way.
var = Model.all
t = var.collect(&:call_at)
hours_arr = []
t.each do |value|
hours_arr << value.time.hour
end

How can I put conditionals in my controller? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm having problems when I'm trying to put a condition in params in my controller. This code is inside my controller:
if params[:example] == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
Is this code correct? How can I put a conditional in params controller?
You code looks okay. Unfortunately you do not say what problem you have. The only thing I see that may fail is the condition. If you pass some value into your params they are not typecasted. Therefore I guess you should use to_i in your condition:
if params[:example].to_i == 1
...
Try with
#table = Model.where((params[:example] == 1 ? 'column_table= ?' : 'column_table2 = ?'), params[:example]) unless params[:example].blank?
Here is code. You have save params[:example] in one variable.and use your coditions.
if params[:example].present?
#example = params[:example]
if #example == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
You can also send the column name along with its value and avoid ifs
#table = Model.find(:all,:conditions=>["#{params[:col]} = ?", params[:example] ] )

Resources