Chapter 7: Strings and Things

7.1 Invoking methods on objects

#!/usr/bin/env ruby

fred = "c"
if fred == "c"
  puts fred
end

########################################

fruit = "banana"
letter = fruit[1, 1]
puts letter

########################################

letter = fruit[0, 1]
#puts letter

7.2 Length

#!/usr/bin/env ruby

fruit = "banana"
length = fruit.length

########################################

last = fruit[length, 1] # WRONG!!

########################################

last = fruit[length-1, 1]
#puts last

# Teach here about negative indexing in Ruby.
# Last character is fruit[-1, 1]
#puts fruit[-1, 1]  # index and number of chars
#puts fruit[-1..-1] # spiffy range

7.3 Traversal

#!/usr/bin/env ruby

fruit = "banana"

index = 0
while index < fruit.length
  letter = fruit[index, 1]
  puts letter
  index = index + 1
end

=begin
fruit.each_byte do |ascii|
  puts ascii.chr
end

for i in 0...fruit.length
  puts fruit[i, 1]
end
=end

7.4 Run-time errors

7.5 Reading documentation

7.6 The indexOf method

#!/usr/bin/env ruby

fruit = "banana"

index = fruit.index("a")
#puts index

########################################

index = fruit.index("a", 2)
#puts index

########################################

index = fruit.index("a", 5)
#puts index

7.7 Looping and counting

#!/usr/bin/env ruby

fruit = "banana"
length = fruit.length
count = 0
index = 0
while index < length
  if fruit[index, 1] == "a"
    count = count + 1
  end
  index = index + 1
end
puts count

=begin
puts fruit.count("a")
=end

########################################

# As an exercise, encapsulate this code in a method named countLetters, and generalize it so that it accepts the string and the letter as arguments.

def count_letters(string, letter)
  length = string.length
  count = 0
  index = 0
  while index < length
    count = count + 1 if string[index, 1] == letter
    index = index + 1
  end
  return count
end

puts count_letters("banana", "a")

=begin
def count_letters(string, letter)
  return string.count(letter)
end

puts count_letters("banana", "a")
=end

########################################

# As a second exercise, rewrite the method so that it uses indexOf to locate the a's, rather than checking the characters one by one.

def count_letters(string, letter)
  count = 0
  index = string.index(letter, 0)
  while index
    count = count + 1
    index = string.index(letter, index + 1)
  end
  return count
end

puts count_letters("banana", "a")
puts count_letters("banana", "b")
puts count_letters("banana", "c")

7.8 Increment and decrement operators

#!/usr/bin/env ruby

fruit = "banana"
length = fruit.length
count = 0

index = 0
while index < length
  if fruit[index, 1] == "a"
    count += 1
  end
  index += 1
end

puts count

7.9 Strings are immutable

#!/usr/bin/env ruby

name = "Alan Turing"
upper_name = name.upcase

puts name
puts upper_name

=begin
name.upcase!
puts name
=end

7.10 Strings are incomparable

#!/usr/bin/env ruby

name1 = "Alan Turing"
name2 = "Ada Lovelace"

if name1 == name2
  puts "The names are the same."
end

flag = (name1 <=> name2)
if flag == 0
  puts "The names are the same."
elsif flag < 0
  puts "name1 comes before name2."
elsif flag > 0
  puts "name2 comes before name1."
end

=begin
if name1 == name2
  puts "The names are the same."
elsif name1 < name2
  puts "name1 comes before name2."
elsif name1 > name2
  puts "name2 comes before name1."
end
=end

7.11 Glossary

7.12 Exercises

GNU Free Documentation License Valid HTML Valid CSS Built with BBEdit
Friendly links for Google “juice”