On this episode of Ruby for All, Andrew and Julie are having an ADHD day since Andrew’s had a lot going on with work stuff and Julie’s been hyperfocused learning about the mobile app. But that won’t stop them from discussing their favorite Ruby Methods. Today, they’re excited to talk about the basics of Strings in programming because Andrew loves a good string. They’ll be discussing the Ruby Methods chomp, strip, split, valid_encoding, sub, gsub, slice, delete_prefix, and swapcase. Although some of these may seem pretty hard to understand, they hope after listening you’ll have a little better understanding of the fundamentals of a String. Go ahead and download this episode now!
[00:01:50] We start with the String method chomp, which returns a new String with the given record separator removed from the end of string. This is one of the first methods Julie learned when she was learning Ruby.
[00:03:37] When does Andrew use chomp? Also, he tells us how strip is different from chomp, and Julie shares an annoying thing about space after the email.
[00:06:26] The next method is split, which divides the string into substrings based on a delimiter, returning an array of these substrings. Andrew shares an example.
[00:10:21] On to method valid encoding, where Andrew explains why the valid encoding we want in Ruby is UTF-8, and he tells us how they were getting errors in their error monitoring service in Honeybadger.
[00:13:21] Moving on to the next method sub, which returns a copy of self with only the first occurrence (not all occurrences) of the given pattern replaced. Julie gives us a great example.
[00:14:22] The next method is gsub, which Andrew uses more than sub, and we find out how gsub is different from sub and the main difference being it replaces all occurrences. Andrew mentions a great article to read (link below) that talks about gsub with a block.
[00:15:49] Our next method is slice, which returns the substring of self, specified by the arguments, and Julie mentions that string slice is an alias for string brackets and Andrew did not know that! Does Andrew use slice or bracket?
[00:18:39] Delete_prefix is the next method and a little easier to understand. It returns a copy of string with leading prefix deleted. Andrew gives an example.
[00:20:38] Julie really likes the last method which is swapcase. Why does she like it? Andrew thinks it’s one of those fun Ruby methods.
Panelists:
Andrew Mason
Julie J.
Sponsors:
GoRails
Honeybadger
Links:
Andrew Mason Twitter
Andrew Mason Website
Julie J. Twitter
Julie J. Website
chomp
strip
split
valid_encoding
sub
gsub
Using Ruby’s gsub with a block-Bozhidar Batsov
slice
delete_prefix
swapcase