On Sunday, February 4, 2018 at 7:00:17 PM UTC+1, Jesus Castello wrote:
Hello!
I wrote an article about ruby string methods on my blog rubyguides.
You can read it here:
http://www.rubyguides.com/2018/01/ruby-string-methods/
Let me know what you think :)
Looks good. Just a few minor nitpicks:
My preferred method to test whether a string represents a number is to actually convert it and deal with the error:
begin
i = Integer(str)
printf "%d\n", i
rescue ArgumentError
puts "Oh, it's not a number!"
end
Or
i = Integer(str) rescue nil
if i ...
There is another way to extract a sub string:
substr = str[/fo+/]
and for replacing parts of the string:
str[/fo+/] = "bar"
(You'll get an IndexError if there is no match.)
Cheers
robert
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)