├── Gemfile ├── Gemfile.lock ├── blocks.rb ├── config.ru ├── count.rb ├── day.rb ├── functional.rb ├── hello_app.rb ├── palindrome.rb ├── palindrome_file ├── palindrome_url ├── palindromes.txt ├── phrases.txt └── wikp /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'sinatra' 3 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | mustermann (1.0.2) 5 | rack (2.0.5) 6 | rack-protection (2.0.1) 7 | rack 8 | sinatra (2.0.1) 9 | mustermann (~> 1.0) 10 | rack (~> 2.0) 11 | rack-protection (= 2.0.1) 12 | tilt (~> 2.0) 13 | tilt (2.0.8) 14 | 15 | PLATFORMS 16 | ruby 17 | 18 | DEPENDENCIES 19 | sinatra 20 | 21 | BUNDLED WITH 22 | 1.16.1 23 | -------------------------------------------------------------------------------- /blocks.rb: -------------------------------------------------------------------------------- 1 | def sandwich 2 | puts "top bread" 3 | yield 4 | puts "bottom bread" 5 | end 6 | 7 | sandwich do 8 | puts "mutton, lettuce, and tomato" 9 | end 10 | 11 | def bad_sandwich(contents) 12 | puts "top bread" 13 | contents 14 | puts "bottom bread" 15 | end 16 | 17 | bad_sandwich(puts "mutton, lettuce, and tomato") 18 | 19 | def tag(tagname, text) 20 | html = "<#{tagname}>#{text}" 21 | yield html 22 | end 23 | 24 | # Wrap some text in a paragraph tag. 25 | tag("p", "Lorem ipsum dolor sit amet") do |markup| 26 | puts markup 27 | end 28 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | require './hello_app' 2 | run Sinatra::Application 3 | -------------------------------------------------------------------------------- /count.rb: -------------------------------------------------------------------------------- 1 | sonnet = "Let me not to the marriage of true minds 2 | Admit impediments. Love is not love 3 | Which alters when it alteration finds, 4 | Or bends with the remover to remove. 5 | O no, it is an ever-fixed mark 6 | That looks on tempests and is never shaken 7 | It is the star to every wand'ring bark, 8 | Whose worth's unknown, although his height be taken. 9 | Love's not time's fool, though rosy lips and cheeks 10 | Within his bending sickle's compass come: 11 | Love alters not with his brief hours and weeks, 12 | But bears it out even to the edge of doom. 13 |   If this be error and upon me proved, 14 |   I never writ, nor no man ever loved." 15 | 16 | # Unique words 17 | uniques = {} 18 | # All words in the text 19 | words = sonnet.scan(/\w+/) 20 | 21 | # Iterate through `words` and build up a hash of unique words. 22 | words.each do |word| 23 | if uniques[word] 24 | uniques[word] += 1 25 | else 26 | uniques[word] = 1 27 | end 28 | end 29 | 30 | puts uniques 31 | -------------------------------------------------------------------------------- /day.rb: -------------------------------------------------------------------------------- 1 | require 'date' 2 | 3 | # Returns the day of the week for the given Time object. 4 | def day_of_the_week(time) 5 | Date::DAYNAMES[time.wday] 6 | end 7 | 8 | # Returns a friendly greeting. 9 | def greeting(time) 10 | "Hello, world! Happy #{day_of_the_week(time)}." 11 | end 12 | -------------------------------------------------------------------------------- /functional.rb: -------------------------------------------------------------------------------- 1 | states = ["Kansas", "Nebraska", "North Dakota", "South Dakota"] 2 | 3 | # Returns a URL-friendly version of a string. 4 | # Example: "North Dakota" -> "north-dakota" 5 | def urlify(string) 6 | string.downcase.split.join('-') 7 | end 8 | 9 | # map: Imperative version 10 | def imperative_map(states) 11 | url_states = [] 12 | states.each do |state| 13 | url_states << urlify(state) 14 | end 15 | url_states 16 | end 17 | puts imperative_map(states).inspect 18 | 19 | # map: Functional version 20 | def functional_map(states) 21 | states.map { |state| urlify(state) } 22 | end 23 | puts functional_map(states).inspect 24 | 25 | # select: Imperative version 26 | def imperative_filter(states) 27 | singleWordStates = [] 28 | states.each do |state| 29 | if (state.split(/\s+/).length == 1) 30 | singleWordStates.push(state) 31 | end 32 | end 33 | singleWordStates 34 | end 35 | puts imperative_filter(states).inspect 36 | 37 | # select: Functional version 38 | def functional_filter(states) 39 | states.select { |state| state.split.length == 1 } 40 | end 41 | puts functional_filter(states).inspect 42 | 43 | numbers = (1..10) 44 | 45 | # reduce: Iterative solution 46 | def iterative_sum(numbers) 47 | total = 0 48 | numbers.each do |n| 49 | total += n 50 | end 51 | return total 52 | end 53 | puts iterative_sum(numbers) 54 | 55 | # reduce: Functional solution 56 | def functional_sum(numbers) 57 | numbers.reduce { |total, n| total += n } 58 | end 59 | puts functional_sum(numbers) 60 | 61 | # reduce object: Imperative solution 62 | def imperative_lengths(states) 63 | lengths = {} 64 | states.each do |state| 65 | lengths[state] = state.length 66 | end 67 | return lengths 68 | end 69 | puts imperative_lengths(states) 70 | 71 | # reduce object: Functional solution 72 | def functional_lengths(states) 73 | states.reduce({}) do |lengths, state| 74 | lengths[state] = state.length 75 | lengths 76 | end 77 | end 78 | puts functional_lengths(states) 79 | -------------------------------------------------------------------------------- /hello_app.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra' 2 | require './day' 3 | 4 | DAYNAMES = ["Sunday", "Monday", "Tuesday", "Wednesday", 5 | "Thursday", "Friday", "Saturday"] 6 | 7 | get '/' do 8 | greeting(Time.now) 9 | end 10 | -------------------------------------------------------------------------------- /palindrome.rb: -------------------------------------------------------------------------------- 1 | class String 2 | 3 | # Processes content for palindrome testing. 4 | def processed_content 5 | self.downcase 6 | end 7 | 8 | # Returns true for a palindrome, false otherwise. 9 | def palindrome? 10 | processed_content == processed_content.reverse 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /palindrome_file: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'mhartl_palindrome' 3 | 4 | text = File.read("phrases.txt") 5 | text.split("\n").each do |line| 6 | if line.palindrome? 7 | puts "palindrome detected: #{line}" 8 | end 9 | end 10 | 11 | lines = File.readlines("phrases.txt") 12 | lines.each do |line| 13 | if line.palindrome? 14 | puts "palindrome detected: #{line}" 15 | end 16 | end 17 | 18 | palindromes = File.readlines('phrases.txt').select(&:palindrome?) 19 | File.write('palindromes.txt', palindromes.join) 20 | 21 | 22 | # text = fs.readFileSync("phrases.txt", "utf8") 23 | # text.split("\n").each do |line| 24 | # phrase = new Phrase(line) 25 | # if (phrase.palindrome()) { 26 | # puts "palindrome detected:", line 27 | # } 28 | # }) 29 | -------------------------------------------------------------------------------- /palindrome_url: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'open-uri' 3 | require 'mhartl_palindrome' 4 | 5 | lines = open('https://cdn.learnenough.com/phrases.txt').readlines 6 | lines.each do |line| 7 | if line.palindrome? 8 | puts "palindrome detected: #{line}" 9 | end 10 | end 11 | 12 | # File.readlines("phrases.txt").each do |line| 13 | # if line.palindrome? 14 | # puts "palindrome detected: #{line}" 15 | # end 16 | # end 17 | 18 | # palindromes = File.readlines('phrases.txt').select(&:palindrome?) 19 | # File.write('palindromes.txt', palindromes.join) 20 | 21 | 22 | # text = fs.readFileSync("phrases.txt", "utf8") 23 | # text.split("\n").each do |line| 24 | # phrase = new Phrase(line) 25 | # if (phrase.palindrome()) { 26 | # puts "palindrome detected:", line 27 | # } 28 | # }) 29 | -------------------------------------------------------------------------------- /palindromes.txt: -------------------------------------------------------------------------------- 1 | A butt tuba 2 | A man, a plan, a canal—Panama! 3 | A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal–Panama! 4 | A Toyota's a Toyota 5 | Able was I ere I saw Elba. 6 | Ah, Satan sees Natasha 7 | deified 8 | Dennis sinned. 9 | Dennis and Edna sinned. 10 | Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned. 11 | Go hang a salami, I'm a lasagna hog. 12 | level 13 | Madam, I’m Adam. 14 | No "x" in "Nixon" 15 | No devil lived on 16 | Race fast, safe car 17 | racecar 18 | radar 19 | Was it a bar or a bat I saw? 20 | Was it a car or a cat I saw? 21 | Was it a cat I saw? 22 | Yo, banana boy! 23 | -------------------------------------------------------------------------------- /phrases.txt: -------------------------------------------------------------------------------- 1 | A butt tuba 2 | A bad penny always turns up. 3 | A fool and his money are soon parted. 4 | A man, a plan, a canal—Panama! 5 | A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal–Panama! 6 | A Toyota's a Toyota 7 | Able was I ere I saw Elba. 8 | Ah, Satan sees Natasha 9 | deified 10 | Dennis sinned. 11 | Dennis and Edna sinned. 12 | Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned. 13 | Fools rush in where angels fear to tread. 14 | Gather ye rosebuds while ye may. 15 | Go hang a salami, I'm a lasagna hog. 16 | level 17 | Haste makes waste. 18 | If the shoe fits, wear it. 19 | If wishes were fishes then no man would starve. 20 | Madam, I’m Adam. 21 | No "x" in "Nixon" 22 | No devil lived on 23 | Once bitten, twice shy. 24 | Race fast, safe car 25 | racecar 26 | radar 27 | Those who cannot remember the past are condemned to repeat it. 28 | Was it a bar or a bat I saw? 29 | Was it a car or a cat I saw? 30 | Was it a cat I saw? 31 | When in Rome, do as the Romans do. 32 | Yo, banana boy! 33 | -------------------------------------------------------------------------------- /wikp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'open-uri' 3 | require 'nokogiri' 4 | 5 | # Returns the paragraphs from a Wikipedia link, stripped of reference numbers. 6 | # Especially useful for text-to-speech (both native and foreign). 7 | 8 | url = ARGV[0] 9 | doc = Nokogiri::HTML(open(url).read) 10 | doc.css('.reference').each { |reference| reference.remove } 11 | content_array = doc.css('p').map { |paragraph| paragraph.content } 12 | puts content_array.join("\n\n") 13 | 14 | 15 | # #!/usr/bin/env ruby 16 | # require 'open-uri' 17 | # require 'nokogiri' 18 | 19 | 20 | # url = ARGV[0] 21 | # doc = Nokogiri::HTML(open(url).read) 22 | # doc.css('.reference').each(&:remove) 23 | # # paragraphs = doc.css('p').map(&:content) 24 | # puts doc.css('p').map(&:content).join("\n\n") 25 | # # paragraphs.each do |paragraph| 26 | # # content = paragraph.content.strip 27 | # # convert_roman_numerals!(content) 28 | # # puts content + "\n\n" unless content.empty? 29 | # # end 30 | 31 | 32 | --------------------------------------------------------------------------------