├── .codeclimate.yml ├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console ├── rorr └── setup ├── lib ├── rorr.rb ├── rorr │ ├── base.rb │ ├── config.rb │ ├── dont_ask_me.rb │ ├── init.rb │ ├── main.rb │ ├── return_value.rb │ ├── score.rb │ ├── test_pass.rb │ ├── ui.rb │ └── version.rb └── spec_helper.rb ├── rorr.gemspec ├── screenshots └── rorr_demo.gif ├── spec ├── check_answer │ └── normal │ │ ├── 001_spec.rb │ │ ├── 002_spec.rb │ │ ├── 003_spec.rb │ │ ├── 004_spec.rb │ │ ├── 005_spec.rb │ │ ├── 006_spec.rb │ │ ├── 007_spec.rb │ │ ├── 008_spec.rb │ │ ├── 009_spec.rb │ │ └── 010_spec.rb ├── rorr │ ├── config_spec.rb │ ├── score_spec.rb │ └── ui_spec.rb └── spec_helper.rb ├── templates ├── README.erb ├── Report.erb └── play.erb └── topic ├── methods └── normal │ ├── 001.rb │ ├── 002.rb │ ├── 003.rb │ ├── 004.rb │ ├── 005.rb │ ├── 006.rb │ ├── 007.rb │ ├── 008.rb │ ├── 009.rb │ └── 010.rb ├── questions └── normal │ ├── 001.rb │ ├── 002.rb │ ├── 003.rb │ ├── 004.rb │ ├── 005.rb │ ├── 006.rb │ ├── 007.rb │ ├── 008.rb │ ├── 009.rb │ ├── 010.rb │ ├── 011.rb │ ├── 012.rb │ ├── 013.rb │ ├── 014.rb │ ├── 015.rb │ ├── 016.rb │ ├── 017.rb │ ├── 018.rb │ ├── 019.rb │ └── 020.rb ├── rails ├── 001.rb ├── 002.rb ├── 003.rb ├── 004.rb ├── 005.rb ├── 006.rb ├── 007.rb ├── 008.rb ├── 009.rb └── 010.rb └── ruby ├── 001.rb ├── 002.rb ├── 003.rb ├── 004.rb ├── 005.rb ├── 006.rb ├── 007.rb ├── 008.rb ├── 009.rb └── 010.rb /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | engines: 2 | rubocop: 3 | enabled: true 4 | duplication: 5 | enabled: true 6 | config: 7 | languages: 8 | - ruby 9 | ratings: 10 | paths: 11 | - lib/** 12 | - "**.rb" 13 | exclude_paths: 14 | - spec/**/* 15 | - topic/**/* 16 | - "**/vendor/**/*" 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.2.3 5 | before_install: gem install bundler -v 1.11.2 6 | script: bundle exec rspec --color 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'coveralls', require: false 3 | # Specify your gem's dependencies in rorr.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Leon Ji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RorR 2 | 3 | [![Gem Version](https://badge.fury.io/rb/rorr.svg)](https://badge.fury.io/rb/rorr) 4 | [![Build Status](https://travis-ci.org/mgleon08/rorr.svg?branch=master)](https://travis-ci.org/mgleon08/rorr) 5 | [![Coverage Status](https://coveralls.io/repos/github/mgleon08/rorr/badge.svg?branch=master)](https://coveralls.io/github/mgleon08/rorr?branch=master) 6 | [![Code Climate](https://codeclimate.com/github/mgleon08/rorr/badges/gpa.svg)](https://codeclimate.com/github/mgleon08/rorr) 7 | [![security](https://hakiri.io/github/mgleon08/rorr/master.svg)](https://hakiri.io/github/mgleon08/rorr/master) 8 | 9 | RorR(Ruby or Rails) is a test designed to evaluate the Ruby or Rails proficiency and artificial intelligence in a fun, interactive way. 10 | 11 | ![](screenshots/rorr_demo.gif) 12 | 13 | ## Installation 14 | 15 | ``` 16 | $ gem install rorr 17 | ``` 18 | 19 | ## Getting Started 20 | 21 | Then run the `rorr` command to start 22 | 23 | ``` 24 | $ rorr 25 | $ gem install rorr --no-ri #without doc 26 | ``` 27 | 28 | Have three topics 29 | 30 | ```ruby 31 | Welcom to RorR! 32 | 33 | Choose the topic you want to start 34 | 1. Don't ask me Ruby or Rails 35 | 2. What's the return value? 36 | 3. Make all test pass 37 | 0. exit 38 | 39 | > 40 | ``` 41 | 42 | ### Topic1: Don't ask me Ruby or Rails 43 | 44 | This is to check whether you remember where Ruby ends and Rails. 45 | 46 | ```ruby 47 | Welcome to "Don't ask me Ruby or Rails" 48 | Let's check whether you remember where Ruby ends and Rails 49 | 50 | ------------------------------ 51 | Question 1 : 52 | ------------------------------ 53 | 54 | Array(foo: :bar) # => [[:foo, :bar]] 55 | 56 | ------------------------------ 57 | ruby or rails? (Type the ruby or rails to answer, skip to next question, exit to exit) 58 | 59 | > 60 | ``` 61 | 62 | ### Topic2: What's the return value? 63 | 64 | This will show you the several pieces of code. You should type the code returned values to answer. 65 | 66 | ```ruby 67 | Welcome to "What's the return value?" 68 | Let's check how much do you proficiency in ruby or rails 69 | 70 | ------------------------------ 71 | Question 1 : 72 | ------------------------------ 73 | 74 | 1 == 1.0 75 | 76 | ------------------------------ 77 | What's the return value? (Type the Exception or SyntaxError if you expect it will return, skip to next question, exit to exit) 78 | 79 | > 80 | ``` 81 | 82 | You can type `Exception` & `SyntaxError` to answer if you expect it will return. 83 | 84 | 85 | ### Topic3: Make all test pass 86 | 87 | This will create a rorr directory in your current location where you find a `player.rb` and `README` file. 88 | 89 | You can see `README` for instructions and write you answer to `player.rb`. 90 | 91 | Type rorr to check if you've finished. 92 | 93 | ```ruby 94 | Welcome to "Make all tests pass" 95 | Let's check how much do you proficiency in ruby or rails 96 | 97 | Question 001 has been generated. 98 | See the ./rorr/normal/001/README for instructions. 99 | 100 | When you're done editing player.rb, type the rorr to check, skip to next question, exit to exit 101 | > 102 | ``` 103 | 104 | 105 | ### Test Report 106 | 107 | In finish it will show you test report. 108 | 109 | ```ruby 110 | Test Report 111 | 112 | Q. | Corr | Skip | Retry 113 | ------------------------- 114 | 1. | ✓ | | 0 115 | 2. | ✓ | | 0 116 | 3. | ✓ | | 0 117 | 4. | | ✓ | 0 118 | 5. | | ✓ | 0 119 | 6. | ✗ | | 0 120 | 7. | ✗ | | 0 121 | 8. | ✗ | | 0 122 | 9. | ✓ | | 0 123 | 10. | ✓ | | 0 124 | ------------------------- 125 | 10 | 5 | 2 | 0 126 | 127 | Correct Rate: 50.0% 128 | Skip Rate: 20.0% 129 | 130 | Spend Time: 00:00:33 131 | ``` 132 | 133 | ## Run Options 134 | You can run `rorr -h` for help 135 | 136 | ``` 137 | Usage: rorr [options] 138 | -t, --time SECONDS Delay each turn by seconds (default: 0.6) 139 | -n, --number NUMBER Number of questions (default: 10, all = -1) 140 | -s, --solution Show the solution (default: false) 141 | -h, --help Show this message 142 | ``` 143 | 144 | ## Questions Reference 145 | 146 | If you have good questions welcome to pull requests or comments 147 | on GitHub at [`https://github.com/mgleon08/rorr/pulls`](https://github.com/mgleon08/rorr/pulls) 148 | 149 | * [Rails Hurts quiz](http://railshurts.com/quiz/) 150 | * [11 Essential Ruby Interview Questions*](https://www.toptal.com/ruby/interview-questions) 151 | * [15 Questions to Ask During a Ruby Interview](https://gist.github.com/ryansobol/5252653) 152 | * [CoderByte](https://coderbyte.com/) 153 | 154 | ## Contributing 155 | 156 | Bug reports and pull requests are welcome on GitHub at [`https://github.com/mgleon08/rorr/pulls`](https://github.com/mgleon08/rorr/pulls) 157 | 158 | ## Copyright & License 159 | 160 | * Copyright (c) 2016 Leon Ji. See [LICENSE.txt](https://github.com/mgleon08/rorr/blob/master/LICENSE.txt) for further details. 161 | * The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 162 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | desc "Run RSpec" 5 | RSpec::Core::RakeTask.new(:spec) do |t| 6 | t.verbose = false 7 | end 8 | 9 | task :default => :spec 10 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "rorr" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/rorr: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative "../lib/rorr" 3 | 4 | init = Rorr::Init.new(ARGV) 5 | init.run 6 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /lib/rorr.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH << File.dirname(__FILE__) 2 | 3 | require 'optparse' 4 | require 'colorize' 5 | require 'readline' 6 | require 'fileutils' 7 | require 'erb' 8 | require 'rspec' 9 | require 'spec_helper' 10 | require 'coderay' 11 | 12 | require 'rorr/version' 13 | 14 | require 'rorr/base' 15 | require 'rorr/ui' 16 | require 'rorr/init' 17 | require 'rorr/main' 18 | require 'rorr/score' 19 | require 'rorr/config' 20 | require 'rorr/dont_ask_me' 21 | require 'rorr/return_value' 22 | require 'rorr/test_pass' 23 | -------------------------------------------------------------------------------- /lib/rorr/base.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | module Base 3 | def templates_path 4 | File.expand_path('../../../templates', __FILE__) 5 | end 6 | 7 | def read_template(path) 8 | ERB.new(File.read(path), nil, '-').result(binding) 9 | end 10 | 11 | def generate_file_base_path 12 | Config.path_prefix + "/rorr/#{Config.level}" 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/rorr/config.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class Config 3 | @stdin = $stdin 4 | @stdout = $stdout 5 | @delay = 0.6 6 | @level = 'normal' 7 | @number = 9 8 | @path_prefix = '.' 9 | @solution = false 10 | class << self 11 | attr_accessor :stdin, :stdout, :delay, :topic, :level, :number, :path_prefix, :solution 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/rorr/dont_ask_me.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class DontAskMe 3 | def initialize 4 | @dir = %w(ruby rails) 5 | @questions = [] 6 | create_questions 7 | end 8 | 9 | def start 10 | system 'clear' 11 | UI.puts "\nWelcome to #{"\"Don't ask me Ruby or Rails\"".light_cyan}" 12 | UI.puts "Let's check whether you remember where Ruby ends and Rails\n" 13 | Score.start 14 | @questions.each.with_index(1) do |question, index| 15 | UI.question(question[:qu], index) 16 | Score.init(index) 17 | UI.puts "ruby or rails? (Type the #{'ruby'.green} or #{'rails'.green} to answer, #{UI.skip}, #{UI.exit})\n\n" 18 | while answer = UI.gets 19 | case answer.downcase 20 | when question[:ans] 21 | UI.puts "Yes, this is #{question[:ans]}!".green 22 | Score.add_correct 23 | break 24 | when 'ruby', 'rails' 25 | UI.puts "No, this is not #{answer}!".red 26 | Score.add_wrong 27 | break 28 | when 'skip' 29 | UI.puts 'Skip the Question!'.light_blue 30 | Score.add_skip 31 | break 32 | when 'exit' then exit 33 | when '' then UI.puts_with_delay 'Please enter again!'.light_blue 34 | else 35 | UI.puts_with_delay 'Error, Please enter again!'.light_red 36 | Score.add_retry 37 | end 38 | end 39 | Score.add_report 40 | UI.solution(question[:sol]) 41 | end 42 | Score.finish 43 | UI.report 44 | end 45 | 46 | private 47 | 48 | def create_questions 49 | @dir.each do |dir| 50 | Dir[File.expand_path("../../../topic/#{dir}/*.rb", __FILE__)].each do |file| 51 | content = File.open(file).read.split('# solution') 52 | qu = UI.coderay(content[0]) 53 | sol = UI.coderay(content[1]) 54 | @questions << { qu: qu, ans: dir, sol: sol } 55 | end 56 | end 57 | @questions = @questions.shuffle[0..Config.number] 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /lib/rorr/init.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class Init 3 | def initialize(arguments) 4 | @arguments = arguments 5 | end 6 | 7 | def run 8 | parse_options 9 | Rorr::Main.choose 10 | end 11 | 12 | private 13 | 14 | def parse_options 15 | options = OptionParser.new 16 | options.banner = 'Usage: rorr [options]' 17 | options.on('-t', '--time SECONDS', 'Delay each turn by seconds (default: 0.6)') { |seconds| Config.delay = seconds.to_f } 18 | options.on('-n', '--number NUMBER', 'Number of questions (default: 10, all = 0)') { |number| Config.number = (number.to_i -1) } 19 | options.on('-s', '--solution', 'Show the solution (default: false)') { |_number| Config.solution = true } 20 | options.on('-h', '--help', 'Show this message') { puts(options); exit } 21 | options.parse!(@arguments) 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/rorr/main.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class Main 3 | class << self 4 | def choose 5 | system 'clear' 6 | UI.menu 7 | while Config.topic = UI.gets 8 | case Config.topic 9 | when '1' 10 | game = DontAskMe.new 11 | break 12 | when '2' 13 | game = ReturnValue.new 14 | break 15 | when '3' 16 | game = TestPass.new 17 | break 18 | when '0', 'exit' then exit 19 | else 20 | UI.puts_with_delay 'Please enter it again'.light_blue 21 | end 22 | end 23 | UI.sleep_with_setting 24 | game.start 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/rorr/return_value.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class ReturnValue 3 | def initialize 4 | @questions = [] 5 | create_questions 6 | end 7 | 8 | def start 9 | system 'clear' 10 | UI.puts "\nWelcome to #{"\"What's the return value?\"".light_cyan}" 11 | UI.puts "Let's check how much do you proficiency in ruby or rails\n" 12 | Score.start 13 | @questions.each.with_index(1) do |question, index| 14 | UI.question(question[:qu], index) 15 | Score.init(index) 16 | UI.puts "What's the return value? (Type the #{'Exception'.green} or #{'SyntaxError'.green} if you expect it will return, #{UI.skip}, #{UI.exit})\n\n" 17 | while answer = UI.gets 18 | case 19 | when match_ans?(answer, question[:ans]) 20 | UI.puts 'Correct!'.green 21 | Score.add_correct 22 | break 23 | when answer == 'skip' 24 | UI.puts 'Skip the Question!'.light_blue 25 | Score.add_skip 26 | break 27 | when answer == '' then UI.puts_with_delay 'Please enter again!'.light_blue 28 | when answer == 'exit' then exit 29 | else 30 | UI.puts_with_delay 'Error, Please enter again!'.light_red 31 | Score.add_retry 32 | end 33 | end 34 | Score.add_report 35 | UI.solution(question[:sol]) 36 | end 37 | Score.finish 38 | UI.report 39 | end 40 | 41 | private 42 | 43 | def create_questions 44 | UI.puts "\nCreate Questions...\n" 45 | Dir[File.expand_path("../../../topic/questions/#{Config.level}/*.rb", __FILE__)].each do |file| 46 | qu = UI.coderay(File.read(file)) 47 | ans = qu_ans(File.read(file)) 48 | sol = UI.coderay(ans) 49 | @questions << { qu: "#{qu}\n", ans: ans, sol: "\n#{sol}\n" } 50 | end 51 | @questions = @questions.shuffle[0..Config.number] 52 | end 53 | 54 | def qu_ans(code) 55 | eval(code) 56 | rescue SyntaxError 57 | SyntaxError 58 | rescue 59 | Exception 60 | end 61 | 62 | def match_ans?(stdin, answer) 63 | eval(stdin) == answer 64 | rescue 65 | return false 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/rorr/score.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class Score 3 | extend Base 4 | @report = [] 5 | @total = { correct: 0, wrong: 0, skip: 0, retry: 0 } 6 | class << self 7 | attr_reader :single, :total, :report, :time, :start_time, :finish_time 8 | 9 | def init(index) 10 | @single = { question: "#{index}.", correct: '', skip: '', retry: 0, color: '' } 11 | end 12 | 13 | def add_correct 14 | total[:correct] += 1 15 | single[:correct] = '✓' 16 | single[:color] = 'green' 17 | end 18 | 19 | def add_wrong 20 | total[:wrong] += 1 21 | single[:correct] = '✗' 22 | single[:color] = 'red' 23 | end 24 | 25 | def add_skip 26 | total[:skip] += 1 27 | single[:skip] = '✓' 28 | single[:color] = 'light_blue' 29 | end 30 | 31 | def add_retry 32 | total[:retry] += 1 33 | single[:retry] += 1 34 | end 35 | 36 | def add_report 37 | report << single 38 | end 39 | 40 | def total_count 41 | total[:correct] + total[:wrong] + total[:skip] 42 | end 43 | 44 | def correct_rate 45 | ((total[:correct].to_f / total_count.to_f) * 100).round(2) 46 | end 47 | 48 | def wrong_rate 49 | ((total[:wrong].to_f / total_count.to_f) * 100).round(2) 50 | end 51 | 52 | def skip_rate 53 | ((total[:skip].to_f / total_count.to_f) * 100).round(2) 54 | end 55 | 56 | def start 57 | @start_time = Time.now 58 | end 59 | 60 | def finish 61 | @finish_time = Time.now 62 | @time = (finish_time - start_time).round(2) 63 | end 64 | 65 | def format_time 66 | Time.at(Score.time).utc.strftime('%H:%M:%S') 67 | end 68 | 69 | def export_report 70 | File.open(generate_file_base_path + '/Report', 'w') do |f| 71 | f.write read_template(templates_path + '/report.erb') 72 | end 73 | end 74 | end 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /lib/rorr/test_pass.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class TestPass 3 | include Base 4 | attr_accessor :index, :questions 5 | 6 | def initialize 7 | @questions = [] 8 | create_questions 9 | end 10 | 11 | def start 12 | system 'clear' 13 | UI.puts "\nWelcome to #{'"Make all tests pass"'.light_cyan}" 14 | UI.puts "Let's check how much do you proficiency in ruby or rails\n" 15 | Score.start 16 | @questions.each.with_index(1) do |question, index| 17 | UI.sleep_with_setting 18 | self.index = index - 1 19 | Score.init(index) 20 | generate_file(index) 21 | while answer = UI.gets 22 | case answer.downcase 23 | when 'rorr' 24 | if check_answer(index) 25 | Score.add_correct 26 | break 27 | end 28 | UI.sleep_with_setting 29 | UI.puts "\nPlease Try Again. Make all tests pass." 30 | UI.puts "Type #{'rorr'.green} to check, #{UI.skip}, #{UI.exit}" 31 | Score.add_retry 32 | when 'skip' 33 | UI.puts 'Skip the Question!'.light_blue 34 | Score.add_skip 35 | break 36 | when 'exit' then exit 37 | else 38 | UI.puts_with_delay 'Please enter again!'.light_blue 39 | end 40 | end 41 | Score.add_report 42 | UI.solution(question[:sol]) 43 | end 44 | Score.finish 45 | Score.export_report 46 | UI.report 47 | end 48 | 49 | private 50 | 51 | def create_questions 52 | Dir[File.expand_path("../../../topic/methods/#{Config.level}/*.rb", __FILE__)].each do |file| 53 | content = File.open(file).read.split("# methods\n") 54 | method = content[1].split("\n")[0] 55 | sol = UI.coderay(content[1]) 56 | @questions << { qu: content[0], me: method, sol: "\n#{sol}" } 57 | end 58 | @questions = @questions[0..Config.number] 59 | end 60 | 61 | def generate_file(index) 62 | generate_question(index) unless File.exist?(generate_file_path(index)) 63 | UI.puts "\nQuestion #{basename(index).light_yellow} has been generated." 64 | UI.puts "See the #{"#{generate_file_path(index)}/README".light_yellow} for instructions." 65 | UI.puts "\nWhen you're done editing player.rb, type the #{'rorr'.green} to check, #{UI.skip}, #{UI.exit}\n" 66 | end 67 | 68 | def generate_question(index) 69 | FileUtils.mkdir_p(generate_file_path(index)) 70 | 71 | File.open(generate_file_path(index) + '/README', 'w') do |f| 72 | f.write read_template(templates_path + '/README.erb') 73 | end 74 | 75 | File.open(generate_file_path(index) + '/play.rb', 'w') do |f| 76 | f.write read_template(templates_path + '/play.erb') 77 | end 78 | end 79 | 80 | def generate_file_path(index) 81 | generate_file_base_path + "/#{basename(index)}" 82 | end 83 | 84 | def check_answer(index) 85 | spec = File.expand_path("../../../spec/check_answer/#{Config.level}/#{basename(index)}_spec.rb", __FILE__) 86 | load "#{generate_file_path(index)}/play.rb" 87 | check = RSpec::Core::Runner.run([spec]) 88 | RSpec.clear_examples 89 | check == 0 ? true : false 90 | end 91 | 92 | def basename(index) 93 | index.to_s.rjust(3, '0') 94 | end 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /lib/rorr/ui.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | class UI 3 | class << self 4 | def puts(msg) 5 | Config.stdout.puts(msg) if Config.stdout 6 | end 7 | 8 | def gets 9 | Config.stdin ? Readline.readline('> ', true) : '' 10 | end 11 | 12 | def puts_with_delay(msg) 13 | sleep_with_setting 14 | puts(msg) 15 | end 16 | 17 | def sleep_with_setting 18 | sleep(Config.delay) 19 | end 20 | 21 | def menu 22 | puts_with_delay "\nWelcom to RorR!".light_red 23 | sleep_with_setting 24 | puts "\nChoose the topic you want to start" 25 | puts "1. Don't ask me Ruby or Rails" 26 | puts "2. What's the return value?" 27 | puts "3. Make all test pass" 28 | puts "0. exit\n\n" 29 | end 30 | 31 | def question(question, index) 32 | sleep_with_setting 33 | puts "\n------------------------------" 34 | puts "Question #{index} :" 35 | puts "------------------------------\n\n" 36 | puts question.to_s 37 | puts "------------------------------\n" 38 | end 39 | 40 | def solution(sol = nil) 41 | puts_with_delay "\nsolution:\n".light_magenta + sol.to_s if sol && Config.solution 42 | puts_with_delay "\nEnter to next" 43 | gets 44 | system 'clear' 45 | end 46 | 47 | def report 48 | system 'clear' 49 | puts_with_delay "\n Test Report \n\n" 50 | sleep_with_setting 51 | puts ' Q. | Corr | Skip | Retry ' 52 | puts '-------------------------' 53 | Score.report.each do |r| 54 | puts " #{repo_rjust(r[:question], 3)} | #{repo_format(r[:correct], r[:color])} | #{repo_format(r[:skip], r[:color])} | #{repo_format(r[:retry])}" 55 | end 56 | puts '-------------------------' 57 | puts " #{repo_rjust(Score.total_count, 3)} | #{repo_format(Score.total[:correct])} | #{repo_format(Score.total[:skip])} | #{repo_format(Score.total[:retry])}" 58 | puts "\n#{repo_rjust('Correct Rate:', 14)} #{repo_rjust(Score.correct_rate, 7)}%".green 59 | puts "#{repo_rjust('Skip Rate:', 14)} #{repo_rjust(Score.skip_rate, 7)}%".light_blue 60 | puts "\n#{repo_rjust('Spend Time:', 14)} #{repo_rjust(Score.format_time, 7)}".light_magenta 61 | puts_with_delay "\nEnter to exit\n" 62 | gets 63 | end 64 | 65 | def repo_format(input, color = 'white') 66 | repo_rjust(input).public_send(color) 67 | end 68 | 69 | def repo_rjust(input, length = 2) 70 | input.to_s.rjust(length) 71 | end 72 | 73 | def skip 74 | "#{'skip'.light_blue} to next question" 75 | end 76 | 77 | def exit 78 | "#{'exit'.red} to exit" 79 | end 80 | 81 | def coderay(input) 82 | CodeRay.scan(input, :ruby).terminal 83 | end 84 | end 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /lib/rorr/version.rb: -------------------------------------------------------------------------------- 1 | module Rorr 2 | VERSION = "0.1.3" 3 | end 4 | -------------------------------------------------------------------------------- /lib/spec_helper.rb: -------------------------------------------------------------------------------- 1 | RSpec.configure do |config| 2 | config.backtrace_exclusion_patterns = [/lib/, /spec/] 3 | config.color = true 4 | end 5 | -------------------------------------------------------------------------------- /rorr.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'rorr/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "rorr" 8 | spec.version = Rorr::VERSION 9 | spec.authors = ["Leon Ji"] 10 | spec.email = ["mgleon08@gmail.com"] 11 | 12 | spec.summary = %q{The easiest way to evaluate the Ruby or Rails proficiency in a interactive way.} 13 | spec.description = %q{This is a test designed to evaluate the Ruby or Rails proficiency and artificial intelligence in a fun, interactive way.} 14 | spec.homepage = "https://github.com/mgleon08/rorr" 15 | spec.license = "MIT" 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|features)/}) } 18 | spec.bindir = "bin" 19 | spec.require_paths = ["lib"] 20 | spec.executables = ["rorr"] 21 | 22 | spec.required_ruby_version = '>= 2.2.0' 23 | 24 | spec.add_development_dependency "bundler", "~> 1.11" 25 | spec.add_development_dependency "rake", "~> 10.0" 26 | spec.add_runtime_dependency "rspec", "~> 3.0" 27 | spec.add_runtime_dependency "colorize", "~> 0.8.1" 28 | spec.add_runtime_dependency "coderay", "~> 1.1" 29 | end 30 | -------------------------------------------------------------------------------- /screenshots/rorr_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgleon08/rorr/1d8b8d259a3a05618e3b5d0c5de9b8e895e276c5/screenshots/rorr_demo.gif -------------------------------------------------------------------------------- /spec/check_answer/normal/001_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#sort_keys' do 2 | it do 3 | hsh = { abc: 'hello', 'another_key' => 123, 4567 => 'third' } 4 | expect(sort_keys(hsh)).to eq ["abc", "4567", "another_key"] 5 | end 6 | it do 7 | hsh = { 12345 => 'hello', 'rorr' => 123, foo: 'bar' } 8 | expect(sort_keys(hsh)).to eq ["foo", "rorr", "12345"] 9 | end 10 | it do 11 | hsh = { a: 1, bbb: 2, cc: 3 } 12 | expect(sort_keys(hsh)).to eq ["a", "cc", "bbb"] 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/002_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#cal_chars' do 2 | it do 3 | str = "ababccabcacbc" 4 | expect(cal_chars(str)).to eq ({ :a=>4, :b=>4, :c=>5 }) 5 | end 6 | it do 7 | str = "oooxoxxoxoxoxoxoxoxo" 8 | expect(cal_chars(str)).to eq ({ :o=>11, :x=>9 }) 9 | end 10 | it do 11 | str = "rorrrorrorr" 12 | expect(cal_chars(str)).to eq ({ :r=>8, :o=>3 }) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/003_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#alphabetize' do 2 | it do 3 | ary = ["Ruby", "Rails", "Python", "PHP", "Swift"] 4 | expect(alphabetize(ary, false)).to eq ["PHP", "Python", "Rails", "Ruby", "Swift"] 5 | end 6 | it do 7 | ary = ["Ruby", "Rails", "Python", "PHP", "Swift"] 8 | expect(alphabetize(ary, true)).to eq ["Swift", "Ruby", "Rails", "Python", "PHP"] 9 | end 10 | it do 11 | ary = ["Foo", "Bar", "Ruby", "Or", "Rails"] 12 | expect(alphabetize(ary, true)).to eq ["Ruby", "Rails", "Or", "Foo", "Bar"] 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/004_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#letter_capitalize' do 2 | it do 3 | str = "hello world" 4 | expect(letter_capitalize(str)).to eq "Hello World" 5 | end 6 | it do 7 | str = "i love ruby or rails" 8 | expect(letter_capitalize(str)).to eq "I Love Ruby Or Rails" 9 | end 10 | it do 11 | str = "this is rorr" 12 | expect(letter_capitalize(str)).to eq "This Is Rorr" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/005_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#simple_adding' do 2 | it do 3 | expect(simple_adding(8)).to eq 36 4 | end 5 | it do 6 | expect(simple_adding(12)).to eq 78 7 | end 8 | it do 9 | expect(simple_adding(128)).to eq 8256 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/check_answer/normal/006_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#first_factorial' do 2 | it do 3 | str = "Hello" 4 | expect(first_factorial(str)).to eq "olleH" 5 | end 6 | it do 7 | str = "I Love RorR" 8 | expect(first_factorial(str)).to eq "RroR evoL I" 9 | end 10 | it do 11 | str = "Foo Bar" 12 | expect(first_factorial(str)).to eq "raB ooF" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/007_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#letter_changes' do 2 | it do 3 | str = "hello*3" 4 | expect(letter_changes(str)).to eq "ifmmp*3" 5 | end 6 | it do 7 | str = "have fun!" 8 | expect(letter_changes(str)).to eq "ibwf gvo!" 9 | end 10 | it do 11 | str = "ruby or rails!" 12 | expect(letter_changes(str)).to eq "svcz ps sbjmt!" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/008_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#longest_word' do 2 | it do 3 | str = "fun&!! time" 4 | expect(longest_word(str)).to eq "time" 5 | end 6 | it do 7 | str = "Hello# @@World" 8 | expect(longest_word(str)).to eq "Hello" 9 | end 10 | it do 11 | str = "ruby or rails" 12 | expect(longest_word(str)).to eq "rails" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/check_answer/normal/009_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#first_factorial' do 2 | it do 3 | expect(first_factorial(4)).to eq 24 4 | end 5 | it do 6 | expect(first_factorial(8)).to eq 40320 7 | end 8 | it do 9 | expect(first_factorial(18)).to eq 6402373705728000 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/check_answer/normal/010_spec.rb: -------------------------------------------------------------------------------- 1 | describe '#fibonacci' do 2 | it do 3 | expect(fibonacci(8)).to eq [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 4 | end 5 | it do 6 | expect(fibonacci(15)).to eq [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] 7 | end 8 | it do 9 | expect(fibonacci(20)).to eq [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946] 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/rorr/config_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Rorr::Config do 4 | subject(:config) { described_class } 5 | 6 | it 'defalut value' do 7 | expect(config.stdin).to eq(STDIN) 8 | expect(config.stdout).to eq(STDOUT) 9 | expect(config.delay).to eq(0.6) 10 | expect(config.level).to eq('normal') 11 | expect(config.number).to eq(9) 12 | expect(config.path_prefix).to eq('.') 13 | expect(config.solution).to eq(false) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /spec/rorr/score_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Rorr::Score do 4 | subject(:score) { described_class } 5 | 6 | it 'default value' do 7 | expect(score.report).to eq([]) 8 | expect(score.total).to eq({ correct: 0, wrong: 0, skip: 0, retry: 0 }) 9 | end 10 | 11 | it '#init' do 12 | score.init(1) 13 | expect(score.single).to eq({ question: "1.", correct: '', skip: '', retry: 0, color:'' }) 14 | end 15 | 16 | it '#add_correct' do 17 | score.add_correct 18 | expect(score.total[:correct]).to eq(1) 19 | expect(score.single[:correct]).to eq('✓') 20 | end 21 | 22 | it '#add_wrong' do 23 | score.add_wrong 24 | expect(score.total[:correct]).to eq(1) 25 | expect(score.single[:correct]).to eq('✗') 26 | end 27 | 28 | it '#add_skip' do 29 | score.add_skip 30 | expect(score.total[:skip]).to eq(1) 31 | expect(score.single[:skip]).to eq('✓') 32 | end 33 | 34 | it '#add_retry' do 35 | score.add_retry 36 | expect(score.total[:retry]).to eq(1) 37 | expect(score.single[:retry]).to eq(1) 38 | end 39 | 40 | it '#add_report' do 41 | score.add_report 42 | expect(score.report.length).to eq(1) 43 | end 44 | 45 | it '#total_count' do 46 | expect(score.total_count).to eq(3) 47 | end 48 | 49 | it '#correct_rate' do 50 | expect(score.correct_rate).to eq(((1.0/3.0)*100).round(2)) 51 | end 52 | 53 | it '#wrong_rate' do 54 | expect(score.correct_rate).to eq(((1.0/3.0)*100).round(2)) 55 | end 56 | 57 | it '#skip_rate' do 58 | expect(score.correct_rate).to eq(((1.0/3.0)*100).round(2)) 59 | end 60 | 61 | it '#finish' do 62 | score.start 63 | time = score.finish 64 | expect(time).to eq((score.finish_time - score.start_time).round(2)) 65 | end 66 | 67 | it '#format_time' do 68 | expect(score.format_time).to eq("00:00:00") 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /spec/rorr/ui_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Rorr::UI do 4 | subject(:ui) { described_class } 5 | 6 | before(:each) do 7 | @in = StringIO.new 8 | @out = StringIO.new 9 | @config = Rorr::Config 10 | @config.stdin = @in 11 | @config.stdout = @out 12 | end 13 | 14 | it "should add puts to out stream" do 15 | ui.puts "foo" 16 | expect(@out.string).to eq("foo\n") 17 | end 18 | 19 | it "should fetch gets from in stream" do 20 | @in.puts "bar" 21 | @in.rewind 22 | allow(ui).to receive(:gets).and_return(@config.stdin.gets.chop!) 23 | expect(ui.gets).to eq("bar") 24 | end 25 | 26 | it "should return empty string if no input" do 27 | @config.stdin = nil 28 | expect(ui.gets).to eq("") 29 | end 30 | 31 | it "should delay after puts when specified" do 32 | @config.delay = 0.8 33 | allow(ui).to receive(:sleep) 34 | ui.puts_with_delay("foo") 35 | expect(ui).to have_received(:sleep) 36 | end 37 | 38 | it "#sleep_with_setting" do 39 | @config.delay = 1 40 | allow(ui).to receive(:sleep_with_setting).and_return(true) 41 | expect(ui.sleep_with_setting).to eq(true) 42 | end 43 | 44 | it "#repo_format" do 45 | expect(ui.repo_format('hello')).to eq("\e[0;37;49mhello\e[0m") 46 | expect(ui.repo_format('hello', 'green')).to eq("\e[0;32;49mhello\e[0m") 47 | end 48 | 49 | it "#repo_rjust" do 50 | expect(ui.repo_rjust(1).class).to eq(String) 51 | expect(ui.repo_rjust(2)).to eq(" 2") 52 | expect(ui.repo_rjust(3, 5)).to eq(" 3") 53 | end 54 | 55 | it "#coderay" do 56 | expect(ui.coderay('#hello')).to eq("\e[1;30m#hello\e[0m") 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'rorr' 3 | require 'coveralls' 4 | Coveralls.wear! 5 | 6 | RSpec.configure do |config| 7 | config.exclude_pattern = "#{File.expand_path('../check_answer/**/*', __FILE__)}" 8 | end 9 | -------------------------------------------------------------------------------- /templates/README.erb: -------------------------------------------------------------------------------- 1 | <%= questions[index][:qu] %> 2 | When you're done editing player.rb, type the rorr command to check. 3 | -------------------------------------------------------------------------------- /templates/Report.erb: -------------------------------------------------------------------------------- 1 | Test Report 2 | 3 | Q. | Corr | Skip | Retry 4 | ------------------------- 5 | <%report.each do |r| -%> 6 | <%="#{r[:question].rjust(3)} |#{r[:correct].rjust(4)} |#{r[:skip].rjust(4)} |#{r[:retry].to_s.rjust(4)}"%> 7 | <%end-%> 8 | ------------------------- 9 | <%="#{total_count.to_s.rjust(3)} |#{total[:correct].to_s.rjust(4)} |#{total[:skip].to_s.rjust(4)} |#{total[:retry].to_s.rjust(4)}"%> 10 | 11 | Correct Rate: <%=correct_rate%>% 12 | Skip Rate: <%=skip_rate%>% 13 | 14 | Spend Time: <%=format_time%> 15 | -------------------------------------------------------------------------------- /templates/play.erb: -------------------------------------------------------------------------------- 1 | <%= questions[index][:me] %> 2 | # add your code here 3 | end 4 | -------------------------------------------------------------------------------- /topic/methods/normal/001.rb: -------------------------------------------------------------------------------- 1 | Write a function that sorts the keys in a hash by the length of the key as a string. 2 | 3 | Example: 4 | 5 | sort_keys({ abc: 'hello', 'another_key' => 123, 4567 => 'third' }) 6 | #=> ["abc", "4567", "another_key"] 7 | sort_keys({ 12345 => 'hello', 'rorr' => 123, foo: 'bar' }) 8 | #=> ["foo", "rorr", "12345"] 9 | sort_keys({ a: 1, bbb: 2, cc: 3 }) 10 | #=> ["a", "cc", "bbb"] 11 | 12 | # methods 13 | def sort_keys(hsh) 14 | hsh.keys.map(&:to_s).sort_by(&:length) 15 | end 16 | -------------------------------------------------------------------------------- /topic/methods/normal/002.rb: -------------------------------------------------------------------------------- 1 | Write a function that calculate the number of characters. 2 | 3 | Example: 4 | 5 | cal_chars("ababccabcacbc") 6 | #=> {:a=>4, :b=>4, :c=>5} 7 | cal_chars("oooxoxxoxoxoxoxoxoxo") 8 | #=> {:o=>11, :x=>9} 9 | cal_chars("rorrrorrorr") 10 | #=> {:r=>8, :o=>3} 11 | 12 | # methods 13 | def cal_chars(str) 14 | ans = Hash.new(0) 15 | str.each_char { |char| ans[char.to_sym] += 1 } 16 | ans 17 | end 18 | -------------------------------------------------------------------------------- /topic/methods/normal/003.rb: -------------------------------------------------------------------------------- 1 | Write a function that sort with array. 2 | 3 | Example: 4 | 5 | alphabetize(["Ruby", "Rails", "Python", "PHP", "Swift"], false) 6 | #=> ["PHP", "Python", "Rails", "Ruby", "Swift"] 7 | alphabetize(["Ruby", "Rails", "Python", "PHP", "Swift"], true) 8 | #=> ["Swift", "Ruby", "Rails", "Python", "PHP"] 9 | alphabetize(["Foo", "Bar", "Ruby", "Or", "Rails"], true) 10 | #=> ["Ruby", "Rails", "Or", "Foo", "Bar"] 11 | 12 | # methods 13 | def alphabetize(ary, bool) 14 | if bool 15 | ary.sort{ |item1, item2| item2 <=> item1} 16 | else 17 | ary.sort{ |item1, item2| item1 <=> item2} 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /topic/methods/normal/004.rb: -------------------------------------------------------------------------------- 1 | Write a function to take the str parameter being passed and capitalize the first letter of each word. 2 | Words will be separated by only one space. 3 | 4 | Example: 5 | 6 | letter_capitalize("hello world") 7 | #=> "Hello World" 8 | letter_capitalize("i love ruby or rails") 9 | #=> "I Love Ruby Or Rails" 10 | letter_capitalize("this is rorr") 11 | #=> "This Is Rorr" 12 | 13 | # methods 14 | def letter_capitalize(str) 15 | str = str.split(" ").each {|a| a.capitalize!}.join(" ") 16 | end 17 | -------------------------------------------------------------------------------- /topic/methods/normal/005.rb: -------------------------------------------------------------------------------- 1 | Write a function to add up all the numbers from 1 to num. 2 | For the test cases, the parameter num will be any number from 1 to 1000. 3 | 4 | Example: 5 | 6 | simple_adding(8) 7 | #=> 36 8 | simple_adding(12) 9 | #=> 78 10 | simple_adding(140) 11 | #=> 8256 12 | 13 | # methods 14 | def simple_adding(num) 15 | (1..num).reduce(0, :+) 16 | end 17 | -------------------------------------------------------------------------------- /topic/methods/normal/006.rb: -------------------------------------------------------------------------------- 1 | Write a function to take the str parameter being passed and return the string in reversed order. 2 | 3 | Example: 4 | 5 | first_factorial("Hello") 6 | #=> "olleH" 7 | first_factorial("I Love RorR") 8 | #=> "RroR evoL I" 9 | first_factorial("Foo Bar") 10 | #=> "raB ooF" 11 | 12 | # methods 13 | def first_factorial(str) 14 | str.split("").reverse.join("") 15 | end 16 | -------------------------------------------------------------------------------- /topic/methods/normal/007.rb: -------------------------------------------------------------------------------- 1 | Write a function to take the str parameter being passed and modify it using the following algorithm. 2 | Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). 3 | 4 | Example: 5 | 6 | letter_changes("hello*3") 7 | #=> "ifmmp*3" 8 | letter_changes("have fun!") 9 | #=> "ibwf gvo!" 10 | letter_changes("ruby or rails!") 11 | #=> "svcz ps sbjmt!" 12 | 13 | # methods 14 | def letter_changes(str) 15 | str.tr!('a-z', 'b-za') 16 | end 17 | -------------------------------------------------------------------------------- /topic/methods/normal/008.rb: -------------------------------------------------------------------------------- 1 | Write a function to take the sen parameter being passed and return the largest word in the string. 2 | If there are two or more words that are the same length, return the first word from the string with that length. 3 | Ignore punctuation and assume sen will not be empty. 4 | 5 | Example: 6 | 7 | longest_word("fun&!! time") 8 | #=> "time" 9 | longest_word("Hello# @@World") 10 | #=> "Hello" 11 | longest_word("ruby or rails") 12 | #=> "rails" 13 | 14 | # methods 15 | def longest_word(str) 16 | str.split(/[^\w]+/).max_by(&:length) 17 | end 18 | -------------------------------------------------------------------------------- /topic/methods/normal/009.rb: -------------------------------------------------------------------------------- 1 | Write a function to take the num parameter being passed and return the factorial of it(e.g. if num = 4, return (4 * 3 * 2 * 1)). 2 | For the test cases, the range will be between 1 and 18. 3 | 4 | Example: 5 | 6 | first_factorial(4) 7 | #=> 24 8 | first_factorial(8) 9 | #=> 40320 10 | first_factorial(18) 11 | #=> 6402373705728000 12 | 13 | # methods 14 | def first_factorial(num) 15 | (1..num).inject(1, :*) 16 | end 17 | -------------------------------------------------------------------------------- /topic/methods/normal/010.rb: -------------------------------------------------------------------------------- 1 | Write a single line of Ruby code that prints the Fibonacci sequence of any length as an array. 2 | 3 | (Hint: use inject) 4 | 5 | Example: 6 | 7 | fibonacci(8) 8 | #=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 9 | fibonacci(15) 10 | #=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] 11 | fibonacci(20) 12 | #=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946] 13 | 14 | # methods 15 | def fibonacci(int) 16 | (1..int).inject( [0, 1] ) { | fib | fib << fib.last(2).inject(:+) } 17 | end 18 | -------------------------------------------------------------------------------- /topic/questions/normal/001.rb: -------------------------------------------------------------------------------- 1 | VAL1 = 'Global' 2 | 3 | module Test 4 | VAL1 = 'Local' 5 | 6 | class Foo 7 | def method1 8 | VAL1 9 | end 10 | end 11 | end 12 | 13 | class Test::Foo 14 | def method2 15 | VAL1 16 | end 17 | end 18 | 19 | Test::Foo.new.method1 20 | -------------------------------------------------------------------------------- /topic/questions/normal/002.rb: -------------------------------------------------------------------------------- 1 | VAL2 = 'Global' 2 | 3 | module Test 4 | VAL2 = 'Local' 5 | 6 | class Foo 7 | def method1 8 | VAL2 9 | end 10 | end 11 | end 12 | 13 | class Test::Foo 14 | def method2 15 | VAL2 16 | end 17 | end 18 | 19 | Test::Foo.new.method2 20 | -------------------------------------------------------------------------------- /topic/questions/normal/003.rb: -------------------------------------------------------------------------------- 1 | 1 == 1.0 2 | -------------------------------------------------------------------------------- /topic/questions/normal/004.rb: -------------------------------------------------------------------------------- 1 | 1.eql?(1.0) 2 | -------------------------------------------------------------------------------- /topic/questions/normal/005.rb: -------------------------------------------------------------------------------- 1 | a = '1' 2 | b = '1' 3 | 4 | a.equal? b 5 | -------------------------------------------------------------------------------- /topic/questions/normal/006.rb: -------------------------------------------------------------------------------- 1 | module Test 2 | def say 3 | "Hi!" 4 | end 5 | end 6 | 7 | class Foo 8 | extend Test 9 | def say 10 | "Hello!" 11 | end 12 | end 13 | 14 | Test.new.say 15 | -------------------------------------------------------------------------------- /topic/questions/normal/007.rb: -------------------------------------------------------------------------------- 1 | module Test 2 | def say 3 | "Hi!" 4 | end 5 | end 6 | 7 | class Foo 8 | extend Test 9 | def say 10 | "Hello!" 11 | end 12 | end 13 | 14 | Foo.say 15 | -------------------------------------------------------------------------------- /topic/questions/normal/008.rb: -------------------------------------------------------------------------------- 1 | class Foo 2 | def say 3 | "Hi!" 4 | end 5 | end 6 | 7 | class Bar < Foo 8 | def say 9 | "Hello! #{super}" 10 | end 11 | end 12 | 13 | Bar.new.say 14 | 15 | -------------------------------------------------------------------------------- /topic/questions/normal/009.rb: -------------------------------------------------------------------------------- 1 | class Foo 2 | def method1 3 | rorr = Foo.new 4 | rorr.method2 5 | end 6 | 7 | protected 8 | 9 | def method2 10 | "Hello World" 11 | end 12 | end 13 | 14 | Foo.new.method1 15 | -------------------------------------------------------------------------------- /topic/questions/normal/010.rb: -------------------------------------------------------------------------------- 1 | class Foo 2 | def method1 3 | rorr = Foo.new 4 | rorr.method2 5 | end 6 | 7 | private 8 | 9 | def method2 10 | "Hello World" 11 | end 12 | end 13 | 14 | Foo.new.method1 15 | -------------------------------------------------------------------------------- /topic/questions/normal/011.rb: -------------------------------------------------------------------------------- 1 | foo, bar = false, 2 2 | foo ||= bar 3 | 4 | foo 5 | -------------------------------------------------------------------------------- /topic/questions/normal/012.rb: -------------------------------------------------------------------------------- 1 | foo, bar = [], 2 2 | foo ||= bar 3 | 4 | foo 5 | -------------------------------------------------------------------------------- /topic/questions/normal/013.rb: -------------------------------------------------------------------------------- 1 | def times_two(arg1); 2 | arg1 * 2; 3 | end 4 | 5 | times_two (5) 6 | -------------------------------------------------------------------------------- /topic/questions/normal/014.rb: -------------------------------------------------------------------------------- 1 | def sum(arg1, arg2); 2 | arg1 + arg2; 3 | end 4 | 5 | sum (1, 2) 6 | -------------------------------------------------------------------------------- /topic/questions/normal/015.rb: -------------------------------------------------------------------------------- 1 | item1 = "foo" 2 | item2 = item1 3 | item1.concat "bar" 4 | 5 | item2 6 | -------------------------------------------------------------------------------- /topic/questions/normal/016.rb: -------------------------------------------------------------------------------- 1 | item1 = "foo" 2 | item2 = item1 3 | item1 += "bar" 4 | 5 | item2 6 | -------------------------------------------------------------------------------- /topic/questions/normal/017.rb: -------------------------------------------------------------------------------- 1 | ary = [] 2 | ary ? true : false 3 | -------------------------------------------------------------------------------- /topic/questions/normal/018.rb: -------------------------------------------------------------------------------- 1 | str = "" 2 | str ? true : false 3 | -------------------------------------------------------------------------------- /topic/questions/normal/019.rb: -------------------------------------------------------------------------------- 1 | -> (a){a}["Hello world"] 2 | -------------------------------------------------------------------------------- /topic/questions/normal/020.rb: -------------------------------------------------------------------------------- 1 | class Foo 2 | def ==(b) 3 | return 'Hi' 4 | end 5 | def size() 6 | self 7 | end 8 | end 9 | 10 | def empty?(s) 11 | return s.size == 0 12 | end 13 | 14 | empty?(Foo.new) 15 | -------------------------------------------------------------------------------- /topic/rails/001.rb: -------------------------------------------------------------------------------- 1 | David Heinemeier Hansson (DHH) 2 | 3 | # solution 4 | David Heinemeier Hansson (born 15 October 1979;known to the Ruby and car racing communities as DHH) is a Danish programmer and the creator of the popular Ruby on Rails web development framework and the Instiki wiki. 5 | 6 | He is also a partner at the web-based software development firm Basecamp (formerly 37signals). 7 | -------------------------------------------------------------------------------- /topic/rails/002.rb: -------------------------------------------------------------------------------- 1 | aaa.instance_values 2 | 3 | # solution 4 | Returns a hash with string keys that maps instance variable names without “@” to their corresponding values. 5 | 6 | class C 7 | def initialize(x, y) 8 | @x, @y = x, y 9 | end 10 | end 11 | 12 | C.new(0, 1).instance_values # => {"x" => 0, "y" => 1} 13 | -------------------------------------------------------------------------------- /topic/rails/003.rb: -------------------------------------------------------------------------------- 1 | 1.in?([1,2]) 2 | 3 | # solution 4 | Returns true if this object is included in the argument. Argument must be any object which responds to #include?. 5 | 6 | characters = ["Konata", "Kagami", "Tsukasa"] 7 | "Konata".in?(characters) # => true 8 | -------------------------------------------------------------------------------- /topic/rails/004.rb: -------------------------------------------------------------------------------- 1 | 'hello'.at(4) # => 'o' 2 | 3 | # solution 4 | If you pass a single integer, returns a substring of one character at that position. 5 | 6 | str = "hello" 7 | str.at(0) # => "h" 8 | str.at(1..3) # => "ell" 9 | str.at(-2) # => "l" 10 | str.at(-2..-1) # => "lo" 11 | str.at(5) # => nil 12 | str.at(5..-1) # => "" 13 | 14 | If a Regexp is given, the matching portion of the string is returned. If a String is given, that given string is returned if it occurs in the string. In both cases, nil is returned if there is no match. 15 | 16 | str = "hello" 17 | str.at(/lo/) # => "lo" 18 | str.at(/ol/) # => nil 19 | str.at("lo") # => "lo" 20 | str.at("ol") # => nil 21 | -------------------------------------------------------------------------------- /topic/rails/005.rb: -------------------------------------------------------------------------------- 1 | alias_attribute 2 | 3 | # solution 4 | Allows you to make aliases for attributes, which includes getter, setter, and query methods. 5 | 6 | class Content < ActiveRecord::Base 7 | # has a title attribute 8 | end 9 | 10 | class Email < Content 11 | alias_attribute :subject, :title 12 | end 13 | 14 | e = Email.find(1) 15 | e.title # => "Superstars" 16 | e.subject # => "Superstars" 17 | e.subject? # => true 18 | e.subject = "Megastars" 19 | e.title # => "Megastars" 20 | -------------------------------------------------------------------------------- /topic/rails/006.rb: -------------------------------------------------------------------------------- 1 | class_attribute 2 | 3 | # solution 4 | Declare a class-level attribute whose value is inheritable by subclasses. Subclasses can change their own value and it will not impact parent class. 5 | 6 | class Base 7 | class_attribute :setting 8 | end 9 | 10 | class Subclass < Base 11 | end 12 | 13 | Base.setting = true 14 | Subclass.setting # => true 15 | Subclass.setting = false 16 | Subclass.setting # => false 17 | Base.setting # => true 18 | -------------------------------------------------------------------------------- /topic/rails/007.rb: -------------------------------------------------------------------------------- 1 | DateTime.current 2 | 3 | # solution 4 | Returns Time.zone.now.to_datetime when Time.zone or config.time_zone are set, otherwise returns Time.now.to_datetime. 5 | -------------------------------------------------------------------------------- /topic/rails/008.rb: -------------------------------------------------------------------------------- 1 | Array.wrap(nil) # => [] 2 | Array.wrap([1, 2, 3]) # => [1, 2, 3] 3 | Array.wrap(0) # => [0] 4 | 5 | # solution 6 | Wraps its argument in an array unless it is already an array (or array-like). 7 | -------------------------------------------------------------------------------- /topic/rails/009.rb: -------------------------------------------------------------------------------- 1 | .blank? 2 | 3 | # solution 4 | An object is blank if it’s false, empty, or a whitespace string. 5 | For example, ”, ‘ ’, nil, [], and {} are all blank. 6 | 7 | This simplifies 8 | 9 | address.nil? || address.empty? 10 | to 11 | address.blank? 12 | -------------------------------------------------------------------------------- /topic/rails/010.rb: -------------------------------------------------------------------------------- 1 | (1..10).include?(3..7) # => true 2 | (1..10).include?(0..7) # => false 3 | 4 | # solution 5 | (1..5).include?(2..3) # => false 6 | require 'active_support/all' 7 | (1..5).include?(2..3) # => true 8 | 9 | Active Support extends these methods so that the argument may be another range in turn. 10 | -------------------------------------------------------------------------------- /topic/ruby/001.rb: -------------------------------------------------------------------------------- 1 | Yukihiro Matsumoto 2 | 3 | # solution 4 | Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. 5 | It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. 6 | -------------------------------------------------------------------------------- /topic/ruby/002.rb: -------------------------------------------------------------------------------- 1 | '[%s]' % 'same old drag' # => '[same old drag]' 2 | 3 | # solution 4 | str % arg → new_str 5 | Format — Uses str as a format specification, and returns the result of applying it to arg. 6 | 7 | "%05d" % 123 #=> "00123" 8 | "%-5s: %08x" % [ "ID", "self.object_id" ] #=> "ID : 200e14d6" 9 | "foo = %{foo}" % { :foo => 'bar' } #=> "foo = bar" 10 | -------------------------------------------------------------------------------- /topic/ruby/003.rb: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5].sample 2 | 3 | # solution 4 | Choose a random element or n random elements from the array. 5 | 6 | a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] 7 | a.sample #=> 7 8 | a.sample(4) #=> [6, 4, 2, 5] 9 | -------------------------------------------------------------------------------- /topic/ruby/004.rb: -------------------------------------------------------------------------------- 1 | Date.today 2 | 3 | # solution 4 | Creates a date object denoting the present day. 5 | -------------------------------------------------------------------------------- /topic/ruby/005.rb: -------------------------------------------------------------------------------- 1 | FileUtils.rm_r 'somedir' 2 | 3 | # solution 4 | rm_r(list, options = {}) 5 | remove files list[0] list[1]… If list[n] is a directory, removes its all contents recursively. 6 | -------------------------------------------------------------------------------- /topic/ruby/006.rb: -------------------------------------------------------------------------------- 1 | a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.. 2 | 3 | # solution 4 | cycle(n=nil) { |obj| block } → nil 5 | 6 | Calls block for each element of enum repeatedly n times or forever if none or nil is given. 7 | If a non-positive number is given or the collection is empty, does nothing. 8 | Returns nil if the loop has finished without getting interrupted. 9 | -------------------------------------------------------------------------------- /topic/ruby/007.rb: -------------------------------------------------------------------------------- 1 | Array(foo: :bar) # => [[:foo, :bar]] 2 | 3 | # solution 4 | Array(arg) → array 5 | Returns arg as an Array. 6 | First tries to call to_ary on arg, then to_a. 7 | 8 | Array(1..5) #=> [1, 2, 3, 4, 5] 9 | -------------------------------------------------------------------------------- /topic/ruby/008.rb: -------------------------------------------------------------------------------- 1 | .downcase! 2 | 3 | # solution 4 | downcase! → str or nil 5 | Downcases the contents of str, returning nil if no changes were made. 6 | 7 | 'AbC'.downcase! #=> "abc" 8 | -------------------------------------------------------------------------------- /topic/ruby/009.rb: -------------------------------------------------------------------------------- 1 | Time.now 2 | 3 | # solution 4 | Alias for Time.new. 5 | Returns a Time object initialized to the current system time. 6 | -------------------------------------------------------------------------------- /topic/ruby/010.rb: -------------------------------------------------------------------------------- 1 | .empty? 2 | 3 | # solution 4 | Returns true if self contains no elements. 5 | 6 | [].empty? #=> true 7 | --------------------------------------------------------------------------------