Write a method second_words(string)
that returns the second word of every sentence in the given string
.
Tip
You can generate paragraphs of random text on https://loremipsum.de/ Show archive.org snapshot .
Write a regular expression that matches a sentence, then call it multiple times.
Write a ClassScanner
class that parses a .rb
file containing a simple Ruby class:
# student.rb
class Student < Person
attr_reader :first_name, :last_name, :disabled
attr_accessor :credits
def full_name
first_name + ' ' + last_name
end
def active?
!@disabled
end
end
The ClassScanner
should work like this:
# main.rb
code = File.read('student.rb')
scanner = ClassScanner.new(code)
scanner.name # => 'Student'
scanner.superclass # => 'Person'
scanner.own_methods # => [:first_name, :last_name, :disabled, :credits, :credits=, :full_name, :active?]
We're practicing regular expressions here, not implement a fully correct Ruby parser. Here are more details to scope what your implementation does and does not need to do:
def ... end
, attr_reader
and attr_accessor
. You don't need to support metaprogramming.attr_reader
, def
or end
do not appear in any strings.attr_reader :one, :two
attr_reader(:one, :two)
attr_reader(:one, 'two')
attr_reader :one
attr_reader :two
attr_reader
and attr_accessor
sit on the same line.ClassScanner#superclass
should return nil
in that case.Info
In practice we would never parse Ruby code like this. For most file formats there are libraries that parse data correctly, like the parser gem Show archive.org snapshot for Ruby code or Nokogiri Show archive.org snapshot for HTML.
Regular expressions are a blunt tool that happens to be good enough much of the time. Read Parsing Html The Cthulhu Way Show archive.org snapshot for more.