I have an array that contains hash in it.
[{"id"=>1353899, "client_id"=>727977, "ticket_id"=>1399613, "ticket_ticket_id"=>632355, "goods_quantity"=>143, "firm"=>nil, "cashregister"=>nil, "user_id"=>5048, "created_at"=>"2021-08-25T13:35:15", "started"=>"2021-07-05", "finished"=>"2022-07-04", "returned"=>nil, "account_summ_id"=>6481426, "account_membership_id"=>nil, "recommended"=>nil, "discount"=>"252.0", "discount_kind"=>1, "discount_comment"=>"Сотрудник", "manual_discount"=>true, "fullprice"=>"456.0", "docnumber"=>nil, "membership_summ"=>"0.0", "name"=>"SMART", "ticket_kind_id"=>1, "price"=>"204.0", "duration"=>12, "time_kind_id"=>4, "freeze_count"=>2, "freeze_days"=>61, "passes"=>nil, "tax_percent"=>"20.0", "client_surname"=>"Smetanin", "client_name"=>"Nikita", "client_phone1"=>"+372 5366 9579", "client_phone2"=>nil, "client_email1"=>"smet.n#hotmail.com", "align_to_month"=>0, "auto_continue"=>0, "stop_auto_continue"=>false, "tax_id"=>68, "day_price"=>nil, "together_with_membership"=>0, "promoaction_id"=>nil, "promocode"=>nil, "pay_schedule_option_id"=>nil, "color"=>"#FF9900", "total_limit_passes"=>0, "guest_visits"=>nil, "debt"=>"0.0", "get_next_debt"=>nil, "full_debt"=>"153.0", "debt_membership"=>0, "goods_rest"=>143, "passes_used"=>0, "guest_visits_used"=>0, "freezes_used"=>0, "freeze_days_used"=>0, "author_name"=>"Smetanin Nikita", "users"=>1, "payed"=>true, "schedule_id"=>1708, "status_freeze"=>false, "trainer_id"=>nil, "need_trainer"=>false, "duration_str"=>"12 Month", "passes_str"=>"Not limited", "next_pay"=>"2021-10-05"}]
How do I get the value of "next_pay"=>"2021-10-05"?
I've tried this, but it doesn't seem to work
#next_pay_data.each{|client_ticket|
next_pay = Luckyfit.request('GET','/client_tickets/payed_finished/' + client_ticket['id'].to_s)
client_ticket['next_pay'] = next_pay[:data]
}
If the array always has 1 hash in it, you can eassily access the hash based on the index like array[0].
So you can change your next_pay variable to return the hash inside the array like this:
next_pay = Luckyfit.request('GET','/client_tickets/payed_finished/' + client_ticket['id'].to_s)[0]
Now the next_pay variable is the hash you want it to be and from there you can access the keys like this:
client_ticket['next_pay'] = next_pay['next_pay']
And in the end your code would look like this:
#next_pay_data.each{|client_ticket|
next_pay = Luckyfit.request('GET','/client_tickets/payed_finished/' + client_ticket['id'].to_s)[0]
client_ticket['next_pay'] = next_pay['next_pay']
}
To make it a bit more readable I would suggest to change some variable names and use string interpolation like this:
#next_pay_data.each{|client_ticket|
next_pay_date = Luckyfit.request('GET',"/client_tickets/payed_finished/#{client_ticket['id']}")[0]['next_pay']
client_ticket['next_pay'] = next_pay_date
}
The below code gets all the next_pay values from the array of hash provided:
hash = [{"id"=>1353899, "client_id"=>727977, "ticket_id"=>1399613, "ticket_ticket_id"=>632355, "goods_quantity"=>143, "firm"=>nil, "cashregister"=>nil, "user_id"=>5048, "created_at"=>"2021-08-25T13:35:15", "started"=>"2021-07-05", "finished"=>"2022-07-04", "returned"=>nil, "account_summ_id"=>6481426, "account_membership_id"=>nil, "recommended"=>nil, "discount"=>"252.0", "discount_kind"=>1, "discount_comment"=>"Сотрудник", "manual_discount"=>true, "fullprice"=>"456.0", "docnumber"=>nil, "membership_summ"=>"0.0", "name"=>"SMART", "ticket_kind_id"=>1, "price"=>"204.0", "duration"=>12, "time_kind_id"=>4, "freeze_count"=>2, "freeze_days"=>61, "passes"=>nil, "tax_percent"=>"20.0", "client_surname"=>"Smetanin", "client_name"=>"Nikita", "client_phone1"=>"+372 5366 9579", "client_phone2"=>nil, "client_email1"=>"smet.n#hotmail.com", "align_to_month"=>0, "auto_continue"=>0, "stop_auto_continue"=>false, "tax_id"=>68, "day_price"=>nil, "together_with_membership"=>0, "promoaction_id"=>nil, "promocode"=>nil, "pay_schedule_option_id"=>nil, "color"=>"#FF9900", "total_limit_passes"=>0, "guest_visits"=>nil, "debt"=>"0.0", "get_next_debt"=>nil, "full_debt"=>"153.0", "debt_membership"=>0, "goods_rest"=>143, "passes_used"=>0, "guest_visits_used"=>0, "freezes_used"=>0, "freeze_days_used"=>0, "author_name"=>"Smetanin Nikita", "users"=>1, "payed"=>true, "schedule_id"=>1708, "status_freeze"=>false, "trainer_id"=>nil, "need_trainer"=>false, "duration_str"=>"12 Month", "passes_str"=>"Not limited", "next_pay"=>"2021-10-05"}]
next_pay_array = hash.map do |value|
value["next_pay"]
end
puts next_pay_array
I have a Hash in the following format:
{"PPS_Id"=>["fe4d7c06-215a-48a7-966d-19ab58976548", "6e90208e-4ab2-4d44-bbaa-9a874bff095b"], "Amount"=>"[\"10000.000\", \"2374.000\"]"}
When I write this data into an excel get the following output.
I want to write the data into the Excel this way:
PPS_Id Amount
fe4d7c06-215a-48a7-966d-19ab58976548 10000.000
6e90208e-4ab2-4d44-bbaa-9a874bff095b 2374.000
How do I convert my current Hash to the below?
{PPS_Id"=>["fe4d7c06-215a-48a7-966d-19ab58976548","Amount"=>"10000.000"},{PPS_Id"=>["6e90208e-4ab2-4d44-bbaa-9a874bff095b","Amount"=>"2374.000"}
Can you please assist.
If you can modify your original hash from
hash = {"PPS_Id"=>["fe4d7c06-215a-48a7-966d-19ab58976548", "6e90208e-4ab2-4d44-bbaa-9a874bff095b"], "Amount"=>"[\"10000.000\", \"2374.000\"]"}
to
hash = {"PPS_Id"=>["fe4d7c06-215a-48a7-966d-19ab58976548", "6e90208e-4ab2-4d44-bbaa-9a874bff095b"], "Amount"=>["10000.000", "2374.000"]}
Note: the last value in the hash is an Array instead of a String.
Then, you can generate an Array of hashes on which you can iterate to fill your excel:
ary = hash.inject([]) do |r, (key, value)|
value.each_with_index do |e, i|
r[i] ||= {}
r[i][key] = e
end
r
end
ary # [{"PPS_Id"=>"fe4d7c06-215a-48a7-966d-19ab58976548", "Amount"=>"10000.000"}, {"PPS_Id"=>"6e90208e-4ab2-4d44-bbaa-9a874bff095b", "Amount"=>"2374.000"}]
If your hash really looks like this :
hash = {"PPS_Id"=>["fe4d7c06-215a-48a7-966d-19ab58976548", "6e90208e-4ab2-4d44-bbaa-9a874bff095b"],
"Amount"=>"[\"10000.000\", \"2374.000\"]"}
you can use scan to parse the floats first :
hash["Amount"] = hash["Amount"].scan(/[\d\-\.]+/)
Your hash will now look like :
{"PPS_Id"=>["fe4d7c06-215a-48a7-966d-19ab58976548", "6e90208e-4ab2-4d44-bbaa-9a874bff095b"],
"Amount"=>["10000.000", "2374.000"]}
To get the table you want, you could just transpose the hash values :
hash.values_at("PPS_Id", "Amount").transpose.each{|id, amount|
puts format("%s\t%.3f", id, amount)
}
It will output :
fe4d7c06-215a-48a7-966d-19ab58976548 10000.000
6e90208e-4ab2-4d44-bbaa-9a874bff095b 2374.000
I am trying to search through an array of objects for a value, but am having trouble getting the find_index to work. In my code below, I am trying to search for the name (joseph) in the array. Is this the best way? I want to return that object after I search and find it.
name = "joseph"
array = [{"login":"joseph","id":4,"url":"localhost/joe","description":null},
{"login":"billy","id":10,"url":"localhost/billy","description":null}]
arrayItem = array.find_index {|item| item.login == name}
puts arrayItem
Your array contains a Hash, with keys that are symbols (in hashes, key: value is a shorthand for :key => value). Therefore, you need to replace item.login with item[:login]:
name = "joseph"
array = [{"login":"joseph","id":4,"url":"localhost/joe","description":nil},
{"login":"billy","id":10,"url":"localhost/billy","description":nil}]
arrayIndex = array.find_index{ |item| item[:login] == name }
puts arrayIndex
The code above retrieves the index at which the sought object is in the array. If you want the object and not the index, use find instead of find_index:
arrayItem = array.find{ |item| item[:login] == name }
Also, note that in Ruby, null is actually called nil.
I have a array which is inside a hash. I want know the result of the student (pass/fail) using the following array. First I have to match them with particular standard and compare their marks with the hash pass and fails. And I want to get the key pass or fail based on their mark. How to achieve this using Ruby?
array = [
{
:standard =>1
:pass=>{:tamil=>30,:eng=>25,:math=>35},
:fail=>{:tamil=>10,:eng=>15,:maths=>20}
},
{
:standard =>2,
:pass=>{:tamil=>40,:eng=>35,:math=>45},
:fail=>{:tamil=>20,:eng=>25,:maths=>30}
}
]
#student is assumed to be defined
standard = array.select {|standard| standard[:standard] == #student.standard}
eng_pass = #student.eng_mark >= standard[:pass][:eng]
eng_fail = #student.eng_mark <= standard[:fail][:eng]
return [eng_pass, eng_fail, whatever_else_you_want]
So on and forth for various topics.
The syntax in reading values from this structure is something like:
array[0][:pass][:eng]
and accordingly you can do the comparison as usual in batch:
for i in 0..#students_array.length
num = # student's score
standard = # something like array[0][:pass][:eng]
if num > standard
# something like 'put "You passed!"'
end
end
HI
is it possible to return an array that contains an array and hash from a method in ruby?
i.e
def something
array_new = [another_thing, another_thing_2]
hash_map = get_hash()
return [array_new, hash_map]
end
and to retrieve the array:
some_array, some_hash = something()
thanks
Sure, that's perfectly possible and works exactly as in your example.
You will only ever be able to return one thing. What you are returning there is an array containing an array and a hash.
Ruby methods can be treated as if they return multiple values so you can collect the items in an array or return them as separate objects.
def something
array_new = Array.new
hash_new = Hash.new
return array_new, hash_new
end
a, b = something
a.class # Array
b.class # Hash
c = something
c.class # Array