Memory size of a hash or other object?
ObjectSpace.memsize_of
does work in 1.9.3, documented or not:
puts RUBY_VERSION #=>1.9.3
require 'objspace'
p ObjectSpace.memsize_of("a"*23) #=> 23
p ObjectSpace.memsize_of("a"*24) #=> 24
p ObjectSpace.memsize_of("a".*1000) #=> 1000
h = {"a"=>1, "b"=>2}
p ObjectSpace.memsize_of(h) #=> 116
Ruby finding data type of keys in hash
Use all?
instead each:
config.is_a?(Hash) && config['points'].keys.all? {|key| key.is_a? Integer}
Note: Is there any reason why you're checking if config is a Hash? If it comes from yml file than this is redundant.
To tell whether there is a better way to do this, you would need to show the method you want pass this to and exactly how you are planning on calling it.
Get only Hexadecima values (bytes) from array in ruby
Given the comment, I assume that you really want to ask about matching pattern "space, hex, hex" up to the first non-match.
This would be like
a.map(&:chr).join.match(/^( \X\X)+/)[0]
It uses the special \X
placeholder for regular expressions that matches u̶p̶p̶e̶r̶c̶a̶s̶e̶ hex digits (0-9,A-F,a-f).
Additional info:
Again based on my interpretation of the question, if the original array is long (or a stream) there is no need to consume it all. You should better stop generating characters as soon as possible:
hexs = "0123456789ABCDEF".split.map(&:ord)
a.
lazy.
each_slice(3).
take_while { |spc, h1, h2| spc == 32 && hexs.include?(h1) && hexs.include?(h2) }.
flat_map(&:chr).
to_a.
join
This way any piece of your integer array is not even taken into account.
Ruby get the size in bytes of an array
Like WTP said, you probably intend on returning the size of the JSON representation instead of ruby representation of the array, because the JSON is the actual response to the browser. You can do this by encoding beforehand (yielding a string) and then checking its size.
response['Content-Length'] = ActiveSupport::JSON.encode(items).size
More about JSON serialization and rails
How do I get a value from a hash that is in the array?
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
}
SHA2 and byte management in Ruby
The sha
should not be generated via .to_s
, you need the binary string version. In addition you are feeding more and more blocks into the same digest, whilst your exercise is specifically about a process for doing the same thing but under your own control (i.e. in your own code).
So instead of maintaining a digest
object, and calling .to_s
on it to fetch each sub-hash, you should calculate the hash fresh each time using the Digest::SHA2.digest( data )
class method
Try this instead:
video_chunks, sha = chunker, ''
video_chunks.reverse_each { |chunk| sha = Digest::SHA2.digest( chunk+sha ) }
# Convert to hex:
puts sha.unpack('H*').first
Related Topics
How to Use Link_To to Link an Image and a Text
Installing Ruby Gem Less-Rails on Windows MAChine Using Therubyracer
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
Rails: How to Check If a Column Has a Value
Ruby on Rails: How to Use Oauth2::Accesstoken.Post
Getaddrinfo Error with Mechanize
How to Share State Between Scenarios Using Cucumber
Ruby Errors with Os X Yosemite
Running into Issues with Rvm During Ruby Install (1.9.2)
How to Source Environment Variables for a Command Shell in a Ruby Script
Ruby Create Tar Ball in Chunks to Avoid Out of Memory Error
Expected #Count to Have Changed by 1, But Was Not Given a Block
What Do 'Def +@' and 'Def -@' Mean
How to Integrate 'Premailer' with Rails
Rails 3.2 Activeadmin 'Collection Is Not a Paginated Scope.' Error
How to Split a String into Consecutive Substrings of Length at Most 3 in All Possible Ways
Disable Rdoc and Ri Generation by Default for Rubygems 1.8.X