Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7395

Re: Ultimate Guide to Ruby Strings

Newsgroups comp.lang.ruby
Date 2018-07-05 09:10 -0700
References <442165f5-c8c9-4273-8762-65a86a23bc5c@googlegroups.com>
Message-ID <7f511e6f-cadd-4048-b2b2-e153ca86eb64@googlegroups.com> (permalink)
Subject Re: Ultimate Guide to Ruby Strings
From Robert Klemme <shortcutter@googlemail.com>

Show all headers | View raw


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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Find similar


Thread

Ultimate Guide to Ruby Strings Jesus Castello <matugm@gmail.com> - 2018-02-04 10:00 -0800
  Re: Ultimate Guide to Ruby Strings Fernando Basso <fernandobasso.br@gmail.com> - 2018-06-17 08:28 -0300
  Re: Ultimate Guide to Ruby Strings Robert Klemme <shortcutter@googlemail.com> - 2018-07-05 09:10 -0700

csiph-web