Ruby - Modules 2020
We may want to prefer double quote("") to single quote(''). In the example below demonstrates when we have a variable within the quote, we should use double quote:
#!/usr/bin/ruby # quote.rb # How to quote puts "Adding two numbers (4+5) = #{4+5} \n\n" puts 'Adding two numbers (4+5) = #{4+5} \n\n'
Output:
$ ./quote.rb Adding two numbers (4+5) = 9 Adding two numbers (4+5) = #{4+5} \n\n
As we can see if we use single quote(''), it takes the string as literal, and does not replacing the variables.
How can we define a long string with several lines?
We'll use EOM to tell Ruby it's a multiline string:
#!/usr/bin/ruby # multi.rb # How to multiline string long_string = <<EOM This is a really long string that requires several lines \n\n EOM puts long_string
Output:
$ ./multi.rb This is a really long string that requires several lines
In the following code, we do all sorts of operation on strings:
#!/usr/bin/ruby # string.rb # How to string operations first_name = "Edwin" last_name = "Hubble" # simple string concatenation full_name = first_name + last_name puts "initial full_name = #{full_name}" middle_name = "Powell" # concatenate full_name = "#{first_name} #{middle_name} #{last_name}" puts "full_name with middle_name = #{full_name}" # substring check puts full_name.include?("Powell") # string size puts full_name.size # counting the number of vowels and consonants puts "Vowels : " + full_name.count("aeiou").to_s puts "Consonants : " + full_name.count("^aeiou").to_s # check start puts full_name.start_with?("Hubble") # index of a substring puts "Index : " + full_name.index("Hubble").to_s
Output:
$ ./string.rb initial full_name = EdwinHubble full_name with middle_name = Edwin Powell Hubble true 19 Vowels : 5 Consonants : 14 false Index : 13
,equal? does object comparison. So, if same object it returns true.
#!/usr/bin/ruby # comp.rb # How to comparison operation with .equal? # different objects puts "\"a\".equal?(\"a\") : " + ("a".equal?"a").to_s # same object a = "a" puts "a.equal?a : " + (a.equal?a).to_s
Output:
$ ./comp.rb "a".equal?("a") : false a.equal?a : true
We can control the cases:
#!/usr/bin/ruby # case.rb # How to case operations full_name = "My full name" puts full_name.upcase puts full_name.downcase puts full_name.swapcase
Output:
$ ./case.rb MY FULL NAME my full name mY FULL NAME
Ruby has strip function for spaces: lstrip, rstrip, and strip.
#!/usr/bin/ruby # strip.rb # How to strip name = " myname" puts "name.size = #{name.size}" puts "name = #{name}" puts puts "name.lstrip = #{name.strip}" puts "name.lstrip.size = #{name.lstrip.size}" puts "name.strip = #{name.strip}" puts "strip name.strip.size = #{name.strip.size}" puts name = "myname " puts "name = #{name}" puts "name.size = #{name.size}" puts "name.rstrip.size = #{name.rstrip.size}"
Output:
$ ./strip.rb name.size = 8 name = myname name.lstrip = myname name.lstrip.size = 6 name.strip = myname strip name.strip.size = 6 name = myname name.size = 8 name.rstrip.size = 6
#!/usr/bin/ruby # just.rb # How to just name = "Edwin Powell Hubble" puts name.rjust(40,'.') puts name.ljust(40,'.') puts name.center(40,'.')
Output:
$ ./just.rb .....................Edwin Powell Hubble Edwin Powell Hubble..................... ..........Edwin Powell Hubble...........
#!/usr/bin/ruby # chop.rb # How to chop/chomp name = "Edwin Powell Hubble" # cut off the last chaaracter puts name.chop # cut off the given string puts name.chomp('bble') puts name.chomp('Hubble')
Output:
$ ./chop.rb Edwin Powell Hubbl Edwin Powell Hu Edwin Powell
In the example below, we'll split a string in two ways: 1. every character 2. space.
#!/usr/bin/ruby # split.rb # How to split name = "Edwin Powell Hubble" # split every character array = name.split(//) puts array # split with a space as a delimiter array = name.split(/ /) puts array
Output:
$ ./split.rb E d w i n P o w e l l H u b b l e Edwin Powell Hubble
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization