Find a letter's alphabetical score [closed] - ruby

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 7 years ago.
Improve this question
What would be the right way to find the location of a character within the alphabet? For example:
"A".find_score # => 1
"C".find_score # => 3

"A".ord
returns 65, the numeric code for "A", which is what the alphabet starts at. If you want it to start at 1 you could just subtract 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
which works like:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3

Related

Regex dynamic string manipulation [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 6 years ago.
Improve this question
Im aiming for a regex formula to return chunks of a string based on a character, if this string contains L1 then its going to be only one chunk, if L2 is found it would return 2 chunks, L3 = 3 chunks.
Example
Lets assume we have this string
"L2N1N1"
and we would like to get 2 string
"L2N1" and "L2N1N1"
Another example
"L3N1N1N2"
to return 3 strings
"L3N1" "L3N1N1" "L3N1N1N2"
Im using Ruby
"L3N1N1N2".sub(/L(\d)(?:N\d)+/) do |m|
$1.to_i.times.map { |i| m[0..3+2*i] }.join(' ')
end
#⇒ "L3N1 L3N1N1 L3N1N1N2"

Converting Fixnum to Array in 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 8 years ago.
Improve this question
How to convert a fixnum to an array in ruby?
For Example:
s = SituationType.all(:conditions => {:name => 'did not match retrieved design - text misspelled'}).collect(&:id)
result: [10034, 10055]
sf = situation_type_id = SituationType.find_by_name('did not match retrieved design - text misspelled').id
result: 10034
s -sf says,
TypeError: can't convert Fixnum into Array
Do the following
s - [sf]
Since sf is an integer, It cannot do the subtraction unless you convert it into an array

Add element to array and generate Hash with two arrays in 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 9 years ago.
Improve this question
Need to add two arrays before that need to add some value at starting of the first array. Look at the following:
#conunty_format = [ "country", "imps", "revenue","network_revenue"]
final_ca = [2000,55.62,88.69]
I need to add "Canada" to final_ca and generate hash with corresponding county_format.
Hash[#conunty_format.zip(final_ca.unshift('canada'))]
=> {"country"=>"canada", "imps"=>2000, "revenue"=>55.62, "network_revenue"=>88.69}
You can use Array Zip and some properties of Array to achieve it in a single line. see the below code.
resulted_hash = #country_format.zip(final_ca.unshift("Canada")).inject({}) do |r, s| r.merge!({s[0] => s[1]}) end

Getting the third number occurrence in 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 9 years ago.
Improve this question
Right now I'm scrapping this:
(178,979)
with this:
.text[/[\d,]+/]
to turn it into this:
178,979
Now I want to scrap this:
Showing 1 - 24 of 512,518 Results
and turn it into this:
512,518
What's the easiest way of accomplishing that?
I'd go about it something like this:
'Showing 1 - 24 of 512,518 Results'[/\d+,\d+/] # => "512,518"
Or this:
'Showing 1 - 24 of 512,518 Results'[/(\S+) Results$/i, 1] # => "512,518"
str = 'Showing 1 - 24 of 512,518 Results'
str.scan(/[\d,]+/)[2]
#=> "512,518"
str[/\d+,\d+/]
#=> "512,518"
You are looking for an integer, followed by a comma, again followed by an integer. So this regex should do the trick:
/\d+,\d+/

Ruby: How do I get items out of an array? [closed]

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
I have this:
array = ["a","b","c"]
How do I get this:
"a","b","c"
I need to get the items out of the array, each double-quoted, separated by a comma.
array.collect { |a| "\"#{a}\"" }.join(",")
I'm just started to learn ruby, I guess:
return ["a","b","c"].map{|i| '"' + i + '"'}.join(",")
Might you want to get this:
irb(main):009:0> [1, 2, 3].map(&:to_s).join('","')
=> "1\",\"2\",\"3"
"a","b","c" this is not abject (these are 3 objects). But in ruby any code returns object value. So you should know what you want to get: 1 object (I returning string in this example) or various. If you want to get 3 objects you should extract array like this:
a, b, c = [1,2,3].map(&:to_s)

Resources