How do I do a reverse to_i? - ruby

I'm using Ruby 2.4. If there are numbers at the beginning of my string, I can get the value as an integer by doing
2.4.0 :003 > s = "13s"
=> "13s"
2.4.0 :004 > s.to_i
=> 13
However, how do I get the numbers as an integer if they are at the end of my string? For instance, in
s13
I have "13" at the end of the string, but obviously, .to_i won't extract that ...
2.4.0 :005 > s = "s13"
=> "s13"
2.4.0 :006 > s.to_i
=> 0
What's a generic way of extracting the numerical portion of the end of a string? IF the string is
abcd
I'd expect the numerical portion to just be zero.

One solution would be this one
"s13".reverse.to_i.to_s.reverse
Or extract the digits from the end of the string
"s13"[/\d+\z/]

Related

what is [-1, 1] in this ruby code?

I am looking at an example of code and confused.
def self.type(input)
input.strip!
return 'question' if input[-1,1] == '?'
end
So, input[-1] makes sense, it is checking if the last character is a question mark. What does the 1 do? Also, all the example tests pass without the 1.
input[-1,1] means reading 1 character from the last character. It gives the same result as input[-1] because you are reading just 1 character from the last character.
Look at some examples to understand more:
❯ irb
2.3.0 :001 > input = 'lenin'
=> "lenin"
2.3.0 :002 > input[-1]
=> "n"
2.3.0 :003 > input[-1,1]
=> "n"
2.3.0 :004 > input[-2]
=> "i"
2.3.0 :005 > input[-2, 1]
=> "i"
2.3.0 :006 > input[-2, 2]
=> "in"
2.3.0 :007 > input[-2, 3]
=> "in"

How do I compare a range to an array?

How do I see if an array matches a range?
[1..3] == [1,2,3] # => false
I've also tried
[1..3].to_a == [1,2,3] # => false
but I'm stumped. Is there any way to coerce a range into an array so I can compare it with one?
ah! Turns out I was getting confused on the syntax.
arr = [1..3] # Actually sets an array with a range as the first element
arr[0] # => 1..3
What I needed was this:
(1..3).to_a == [1,2,3] # => true
2.1.1 :006 > [1..3].class
=> Array
2.1.1 :007 > (1..3).class
=> Range
2.1.1 :008 > (1..3).to_a == [1,2,3]
=> true
hope that solves your issue

Ruby - Array.length returning character count instead of element count

I'm using Ruby 2.0 for a Rails project and am having some issues obtaining the element length of an array in the console.
First example
2.0.0-p353 :001 > search = "test"
=> "test"
2.0.0-p353 :002 > search.split
=> ["test"]
2.0.0-p353 :003 > search.length
=> 4
Second example
2.0.0-p353 :001 > search = "testOne, TestTwo"
=> "testOne, TestTwo"
2.0.0-p353 :002 > search.split(/[\s,]+/)
=> ["testOne", "TestTwo"]
2.0.0-p353 :003 > search.length
=> 16
How do I return the element count instead of the character count?
Well, you're not assigning your split array that's why you're seeing the discrepancy.
What you're actually doing is defining a string search and then trying to manipulate that same string.
Try this
testArray = search.split
testArray.size
>> 1
In the first example you are getting the length of the "test" string, not of the ["test"] array. You should assign it to a variable first.
i.e.:
search = "test" # => "test"
array = search.split # => ["test"]
array.length # => 1

How do I access a value from a JSON string?

I have a variable. When I do puts var_name I get this hash:
"{\"numConnections\": 163}"
But when I try to get that number 163 from the value numConnections it isn't working. Here is what I am trying:
connections = temp_var["\"numConnections\""]
puts connections.inspect
or:
connections = temp_var["numConnections"]
puts connections.inspect
both of which equally don't work.
Any idea how to extract that 163 from there?
If you have a JSON string, you need to parse it into a hash before you can use it to access its keys and values in a hash-like way. Consider this IRB session:
1.9.3p194 :001 > require 'json'
=> true
1.9.3p194 :002 > temp_var = "{\"numConnections\": 163}"
=> "{"numConnections": 163}"
1.9.3p194 :003 > temp_var.class
=> String
1.9.3p194 :004 > JSON.parse(temp_var)
=> {"numConnections"=>163}
1.9.3p194 :005 > JSON.parse(temp_var)['numConnections']
=> 163

What are values mapped to?

I don't understand how Ruby hashes work.
I expect these:
a = 'a'
{a => 1}[a] # => 1
{a: 1}[:a] # => 1
{2 => 1}[2] # => 1
How does this work?
{'a' => 1}['a'] # => 1
The first string 'a' is not the same object as the second string 'a'.
Ruby doesn't use object equality (equal?) for comparing hash keys. It wouldn't be very useful if it did after all.
Instead it uses eql?, which for strings is the same as ==
As a footnote to other answers, you can let a hash behave like you expected:
h = {'a'=> 1}
p h['a'] #=> 1
h.compare_by_identity
p h['a'] #=> nil ; not the same object
some_hash[k] = v
Basically, when you do this, what is stored is not a direct association k => v. Instead of that, k is asked for a hash code, which is then used to map to v.
Equal values yield equal hash codes. That's why your last example works the way it does.
A couple of examples:
1.9.3p0 :001 > s = 'string'
=> "string"
1.9.3p0 :002 > 'string'.hash
=> -895223107629439507
1.9.3p0 :003 > 'string'.hash == s.hash
=> true
1.9.3p0 :004 > 2.hash
=> 2271355725836199018
1.9.3p0 :005 > nil.hash
=> 2199521878082658865

Resources