How do I calculate a String's width in Ruby?
You should use the RMagick gem to render a "Draw" object using the font you want (you can load .ttf files and such)
The code would look something like this:
the_text = "TheTextYouWantTheWidthOf"
label = Draw.new
label.font = "Vera" #you can also specify a file name... check the rmagick docs to be sure
label.text_antialias(true)
label.font_style=Magick::NormalStyle
label.font_weight=Magick::BoldWeight
label.gravity=Magick::CenterGravity
label.text(0,0,the_text)
metrics = label.get_type_metrics(the_text)
width = metrics.width
height = metrics.height
You can see it in action in my button maker here: http://risingcode.com/button/everybodywangchungtonite Determing string actual length not just chars count
The response is here
How do I calculate a String's width in Ruby?
the_text = "TheTextYouWantTheWidthOf"
label = Draw.new
label.font = "Vera" #you can also specify a file name... check the rmagick docs to be sure
label.text_antialias(true)
label.font_style=Magick::NormalStyle
label.font_weight=Magick::BoldWeight
label.gravity=Magick::CenterGravity
label.text(0,0,the_text)
metrics = label.get_type_metrics(the_text)
width = metrics.width
height = metrics.height
How do I calculate the average length of a string in an array?
I would write like:
arr.join.size / arr.size.to_f
Also, you can use sum
since Ruby 2.4:arr.sum(&:size) / arr.size.to_f
#=> 2.0
Rubygame::TTF - The difference between a string's width and the width of all it's characters
Yes: because a good TrueType font, like any good font, will be kerned by the layout engine. http://en.wikipedia.org/wiki/KerningThe result is close, but... Does anyone know why there's a difference of 8 between the sum of the character widths and the string's width?
When doing layout with variable width fonts - which you want - always use the full string width. Per-character numbers are meaningless. (...and, really, the "right" way to do this is to use something that does layout for you. Pango is a project that does that; go count the lines of code, and consider if you really want to write that yourself.)
String count without using .length or .size methods?
Other option, mapping String#chars with index then picking the last:
str = "123456"
str.chars.map.with_index { |_, i| i + 1 }.last
#=> 6
It generates an Array, but we are not looking for efficiency here.Or even using String#index with offset:
str = "aaaa"
str.index(str[-1], -1) + 1
#=> 4
It looks for the index of the latest char starting from the end. How to match a string in array, regardless of the string size in Ruby
Converting strings into array and making an intersection operation should be an option. I know, this is not the best solution, but might save your day.
[17] pry(main)> x = "alto saxophone 1"
=> "alto saxophone 1"
[18] pry(main)> y = "i am not an anglophone"
=> "i am not an anglophone"
[19] pry(main)> z = "alto 1"
=> "alto 1"
[20] pry(main)> x.split & z.split == z.split # & is array intersection
=> true
[21] pry(main)> x.split & y.split == y.split
=> false
Related Topics
How to Access Parent/Sibling Module Methods
What Is Returned in Ruby If The Last Statement Evaluated Is an If Statement
Fresh Ruby Gem from Bundler - Cannot Load My Version.Rb File
Statically Compile Pdftk for Heroku. Need to Split PDF into Single Page Files
How to Remove Header and Footer from Some of The Pages in Ruby on Rails
Starting with Redmine Locally - How Easy Is Migration to Server Later
Converting Ruby Hashes to Arrays
Operator Precedence of Unary Operators
How to Compare Xml Output in a Cucumber Step Using a Multiline String Example
How to Pick The Method to Call When There Is Mixin Method Name Conflict
How to Make Http Delete Request in My Ruby Code Using Net::Http
Implementing a Lookup Table in Rails
Rendering a View from My Ruby on Rails Gem
List Array of Days Between Two Dates
Can't Get Paypal Encrypted Website Payments to Work in Rails