From 5cdf8edee9ebdb64b62a93994cb36e9fec77608f Mon Sep 17 00:00:00 2001 From: Hongzhan Chen Date: Fri, 2 Aug 2019 01:17:30 -0400 Subject: [PATCH] add ruby unit test --- tests/ruby/ruby.bats | 250 ++++++++++++++++++ .../ruby/testcase/arraysearchandsubstitute.rb | 15 ++ tests/ruby/testcase/attaccessor.rb | 11 + tests/ruby/testcase/calllambda.rb | 15 ++ tests/ruby/testcase/casestatement.rb | 11 + tests/ruby/testcase/class.rb | 18 ++ tests/ruby/testcase/classaccount.rb | 36 +++ tests/ruby/testcase/classinheritance.rb | 15 ++ .../ruby/testcase/classscopemethodfunction.rb | 28 ++ tests/ruby/testcase/codeblock.rb | 5 + tests/ruby/testcase/collectmethod.rb | 5 + .../ruby/testcase/convertstringstosymbols.rb | 8 + .../ruby/testcase/convertsymbolstostrings.rb | 2 + tests/ruby/testcase/globalprivatevariable.rb | 23 ++ tests/ruby/testcase/hashhistogram.rb | 9 + tests/ruby/testcase/hashiteration.rb | 7 + .../ruby/testcase/hashlookupsymbolvsstring.rb | 15 ++ tests/ruby/testcase/include.rb | 15 ++ tests/ruby/testcase/lambdamethodsort.rb | 13 + tests/ruby/testcase/methodcapitalize.rb | 10 + tests/ruby/testcase/methodgreeter.rb | 6 + .../testcase/methodprimenumbercalculator.rb | 20 ++ tests/ruby/testcase/modules.rb | 28 ++ tests/ruby/testcase/multipleinheritance.rb | 25 ++ tests/ruby/testcase/privatemethod.rb | 17 ++ tests/ruby/testcase/procs.rb | 18 ++ tests/ruby/testcase/selectivehash.rb | 16 ++ tests/ruby/testcase/sortandorganizearray.rb | 13 + tests/ruby/testcase/soryarraybyalphabet.rb | 8 + tests/ruby/testcase/stringinterpolation.rb | 7 + tests/ruby/testcase/substituion.rb | 13 + tests/ruby/testcase/variablescope.rb | 29 ++ tests/ruby/testcase/virtualcomputer.rb | 23 ++ tests/ruby/testcase/yieldblocklambda.rb | 11 + tests/ruby/testcase/yieldingwithparameters.rb | 10 + 35 files changed, 755 insertions(+) create mode 100755 tests/ruby/ruby.bats create mode 100644 tests/ruby/testcase/arraysearchandsubstitute.rb create mode 100644 tests/ruby/testcase/attaccessor.rb create mode 100644 tests/ruby/testcase/calllambda.rb create mode 100644 tests/ruby/testcase/casestatement.rb create mode 100644 tests/ruby/testcase/class.rb create mode 100644 tests/ruby/testcase/classaccount.rb create mode 100644 tests/ruby/testcase/classinheritance.rb create mode 100644 tests/ruby/testcase/classscopemethodfunction.rb create mode 100644 tests/ruby/testcase/codeblock.rb create mode 100644 tests/ruby/testcase/collectmethod.rb create mode 100644 tests/ruby/testcase/convertstringstosymbols.rb create mode 100644 tests/ruby/testcase/convertsymbolstostrings.rb create mode 100644 tests/ruby/testcase/globalprivatevariable.rb create mode 100644 tests/ruby/testcase/hashhistogram.rb create mode 100644 tests/ruby/testcase/hashiteration.rb create mode 100644 tests/ruby/testcase/hashlookupsymbolvsstring.rb create mode 100644 tests/ruby/testcase/include.rb create mode 100644 tests/ruby/testcase/lambdamethodsort.rb create mode 100644 tests/ruby/testcase/methodcapitalize.rb create mode 100644 tests/ruby/testcase/methodgreeter.rb create mode 100644 tests/ruby/testcase/methodprimenumbercalculator.rb create mode 100644 tests/ruby/testcase/modules.rb create mode 100644 tests/ruby/testcase/multipleinheritance.rb create mode 100644 tests/ruby/testcase/privatemethod.rb create mode 100644 tests/ruby/testcase/procs.rb create mode 100644 tests/ruby/testcase/selectivehash.rb create mode 100644 tests/ruby/testcase/sortandorganizearray.rb create mode 100644 tests/ruby/testcase/soryarraybyalphabet.rb create mode 100644 tests/ruby/testcase/stringinterpolation.rb create mode 100644 tests/ruby/testcase/substituion.rb create mode 100644 tests/ruby/testcase/variablescope.rb create mode 100644 tests/ruby/testcase/virtualcomputer.rb create mode 100644 tests/ruby/testcase/yieldblocklambda.rb create mode 100644 tests/ruby/testcase/yieldingwithparameters.rb diff --git a/tests/ruby/ruby.bats b/tests/ruby/ruby.bats new file mode 100755 index 0000000..5634ef1 --- /dev/null +++ b/tests/ruby/ruby.bats @@ -0,0 +1,250 @@ +#!/usr/bin/env bats +# *-*- Mode: sh; c-basic-offset: 8; indent-tabs-mode: nil -*-* + +load ../utils + +# +# * Array search and substitution +# +@test "array search and substitution" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "echo Lorem | ruby testcase/arraysearchandsubstitute.rb | grep REDACTED" +} + +# +# * Attribute accessor +# +@test "attribute accessor" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/attaccessor.rb | grep Dennis" +} + +# +# * Call lamda +# +@test "call lamda" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/calllambda.rb +} + +# +# * Case statement +# +@test "case statement" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "echo English | ruby testcase/casestatement.rb | grep Hello!" +} + +# +# * Class Account +# +@test "class acount" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/classaccount.rb +} + +# +# * Class inheritance +# +@test "class inheritance" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/classinheritance.rb | grep Meow" +} + +# +# * Basic Class +# +@test "basi class" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/class.rb +} + +# +# * Static functions +# +@test "static functions" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/classscopemethodfunction.rb | grep -E 'eric.*you'" +} + +# +# * Code Block +# +@test "code block" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/codeblock.rb +} + +# +# * Collect Method +# +@test "collect method" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/collectmethod.rb | grep -E '*\!$'" +} + +# +# * Convert string to symbols +# +@test "convert string to symbols" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/convertstringstosymbols.rb +} + +# +# * Convert symbol to string +# +@test "convert symbol to string" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/convertsymbolstostrings.rb | grep 'hello world'" +} + +# +# * Private global variable +# +@test "private global variable" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/globalprivatevariable.rb &>/dev/stdout | grep 'private method'" +} + +# +# * Hash Histogram +# +@test "hash histogram" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "echo 'this is a test of the test' | ruby testcase/hashhistogram.rb" +} + +# +# * Hash Iteration +# +@test "hash iteration" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/hashiteration.rb +} + +# +# * Hash lockup symbol vs string +# +@test "hash lockup symbol vs string" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/hashlookupsymbolvsstring.rb +} + +# +# * Include +# +@test "include" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/include.rb +} + +# +# * Lamda method sort +# +@test "lamda method sort" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/lambdamethodsort.rb +} + +# +# * Method Capitalize +# +@test "method capitalize" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/methodcapitalize.rb | grep -vE '^(r|j)'" +} + +# +# * Method Greeter +# +@test "method greeter" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/methodgreeter.rb | grep 'hello jared nice to meet you!'" +} + +# +# * Prime number calculator +# +@test "method greeter" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/methodprimenumbercalculator.rb +} + +# +# * Module +# +@test "module" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/modules.rb +} + +# +# * Module +# +@test "module" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/multipleinheritance.rb +} + +# +# * Private Method +# +@test "private method" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "ruby testcase/privatemethod.rb &> /dev/stdout | grep 'private method'" +} + +# +# * Procedure +# +@test "Procedure" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/procs.rb +} + +# +# * Selective Hash +# +@test "selective hash" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/selectivehash.rb +} + +# +# * Sort and organize array +# +@test "sort and organize array" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/sortandorganizearray.rb +} + +# +# * Sort array by alphabet +# +@test "sort array by alphabet" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/soryarraybyalphabet.rb +} + +# +# * String interpolation +# +@test "string interpolation" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/stringinterpolation.rb +} + +# +# * String interpolation +# +@test "string interpolation" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby bash -c "echo 'this what it is' | ruby testcase/substituion.rb | grep -v 's'" +} + +# +# * Variable scope +# +@test "variable scope" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/variablescope.rb +} + +# +# * Variable scope +# +@test "variable scope" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/variablescope.rb +} + +# +# * Virtual computer (scope variables) +# +@test "virtual computer (scope variables)" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/virtualcomputer.rb +} + +# +# * Yield block lamda +# +@test "yeald block lamda" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/yieldblocklambda.rb +} + +# +# * Yielding with parameters +# +@test "yealding with parameters" { + docker run --rm -v "$PWD":/src -w /src clearlinux/ruby ruby testcase/yieldingwithparameters.rb +} + diff --git a/tests/ruby/testcase/arraysearchandsubstitute.rb b/tests/ruby/testcase/arraysearchandsubstitute.rb new file mode 100644 index 0000000..2e9756c --- /dev/null +++ b/tests/ruby/testcase/arraysearchandsubstitute.rb @@ -0,0 +1,15 @@ +text = """ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a nisl quis orci mattis vestibulum nec a orci. Sed sit amet libero vel purus tincidunt dictum. Curabitur sit amet quam justo. Nam rutrum tortor a tempor semper. Vivamus rhoncus commodo tortor vel vehicula. Fusce lobortis massa arcu, a ornare metus consequat ac. Nam urna est, vulputate a libero nec, accumsan convallis felis. Cras feugiat arcu tortor, malesuada ultrices arcu pretium a +""" +puts "Word to redact: " +redact = gets.chomp + +words = text.split(" ") + +words.each do |word| + if word != redact + print word + " " + else + print "REDACTED " + end +end diff --git a/tests/ruby/testcase/attaccessor.rb b/tests/ruby/testcase/attaccessor.rb new file mode 100644 index 0000000..f8c453b --- /dev/null +++ b/tests/ruby/testcase/attaccessor.rb @@ -0,0 +1,11 @@ +class Person + attr_accessor :name + + def greeting + "Hello #{@name}" + end +end + +person = Person.new +person.name = "Dennis" +puts person.greeting # => "Hello Dennis" diff --git a/tests/ruby/testcase/calllambda.rb b/tests/ruby/testcase/calllambda.rb new file mode 100644 index 0000000..1e0ec89 --- /dev/null +++ b/tests/ruby/testcase/calllambda.rb @@ -0,0 +1,15 @@ +def lambda_demo(a_lambda) + puts "I'm the method!" + a_lambda.call +end + +lambda_demo(lambda { puts "I'm the lambda!" }) + + +def jared(werba) + puts "still unsure" + werba.call +end + +jared(lambda {puts "why is werba.call inside of the method?"}) +jared(lambda {puts "will this work?!!?!?!!"}) \ No newline at end of file diff --git a/tests/ruby/testcase/casestatement.rb b/tests/ruby/testcase/casestatement.rb new file mode 100644 index 0000000..49fa4c4 --- /dev/null +++ b/tests/ruby/testcase/casestatement.rb @@ -0,0 +1,11 @@ +puts "Hello there!" +greeting = gets.chomp + +# Add your case statement below! +case greeting +when "English" then puts "Hello!" +when "French" then puts "Bonjour" +when "German" then puts "Guten Tag!" +when "Finnish" then putrs "Haloo!" +else puts "i dont know that lang bro" +end \ No newline at end of file diff --git a/tests/ruby/testcase/class.rb b/tests/ruby/testcase/class.rb new file mode 100644 index 0000000..d343e7c --- /dev/null +++ b/tests/ruby/testcase/class.rb @@ -0,0 +1,18 @@ +class Language + def initialize(name, creator) + @name = name + @creator = creator + end + + def description + puts "I'm #{@name} and I was created by #{@creator}!" + end +end + +ruby = Language.new("Ruby", "Yukihiro Matsumoto") +python = Language.new("Python", "Guido van Rossum") +javascript = Language.new("JavaScript", "Brendan Eich") + +ruby.description +python.description +javascript.description \ No newline at end of file diff --git a/tests/ruby/testcase/classaccount.rb b/tests/ruby/testcase/classaccount.rb new file mode 100644 index 0000000..c3f72b5 --- /dev/null +++ b/tests/ruby/testcase/classaccount.rb @@ -0,0 +1,36 @@ +class Account + attr_reader :name, :balance + def initialize(name, balance=100) + @name = name + @balance = balance + end + + def display_balance(pin_number) + puts pin_number == pin ? "Balance: $#{@balance}." : pin_error + end + + def withdraw(pin_number, amount) + if pin_number == pin + @balance -= amount + puts "Withdrew #{amount}. New balance: $#{@balance}." + else + puts pin_error + end + end + + private + + def pin + @pin = 1234 + end + + def pin_error + "Access denied: incorrect PIN." + end +end + +my_account = Account.new("Eric", 1_000_000) +my_account.withdraw(11, 500_000) +my_account.display_balance(1234) +my_account.withdraw(1234, 500_000) +my_account.display_balance(1234) \ No newline at end of file diff --git a/tests/ruby/testcase/classinheritance.rb b/tests/ruby/testcase/classinheritance.rb new file mode 100644 index 0000000..0592dfb --- /dev/null +++ b/tests/ruby/testcase/classinheritance.rb @@ -0,0 +1,15 @@ +class Mammal + def breathe + puts "inhale and exhale" + end +end + +class Cat < Mammal + def speak + puts "Meow" + end +end + +rani = Cat.new +rani.breathe +rani.speak diff --git a/tests/ruby/testcase/classscopemethodfunction.rb b/tests/ruby/testcase/classscopemethodfunction.rb new file mode 100644 index 0000000..8b64712 --- /dev/null +++ b/tests/ruby/testcase/classscopemethodfunction.rb @@ -0,0 +1,28 @@ +class Machine + @@users = {} + + def initialize(username, password) + @username = username + @password = password + @@users[username] = password + @files = {} + end + + def create(filename) + time = Time.now + @files[filename] = time + puts "#{filename} was created by #{@username} at #{time}." + end + + def Machine.get_users + @@users + end +end + +my_machine = Machine.new("eric", 01234) +your_machine = Machine.new("you", 56789) + +my_machine.create("groceries.txt") +your_machine.create("todo.txt") + +puts "Users: #{Machine.get_users}" \ No newline at end of file diff --git a/tests/ruby/testcase/codeblock.rb b/tests/ruby/testcase/codeblock.rb new file mode 100644 index 0000000..148e61d --- /dev/null +++ b/tests/ruby/testcase/codeblock.rb @@ -0,0 +1,5 @@ +1.times do + puts "I'm a code block!" +end + +5.times { puts "As am I!" } \ No newline at end of file diff --git a/tests/ruby/testcase/collectmethod.rb b/tests/ruby/testcase/collectmethod.rb new file mode 100644 index 0000000..ca83d16 --- /dev/null +++ b/tests/ruby/testcase/collectmethod.rb @@ -0,0 +1,5 @@ +abc = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] + +# Add your code below! + +puts abc.collect{ |x| x + "!"} diff --git a/tests/ruby/testcase/convertstringstosymbols.rb b/tests/ruby/testcase/convertstringstosymbols.rb new file mode 100644 index 0000000..b26daf9 --- /dev/null +++ b/tests/ruby/testcase/convertstringstosymbols.rb @@ -0,0 +1,8 @@ +strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"] + +# Add your code below! +symbols = [] +strings.each { |x| symbols.push(x.to_sym) } + +puts symbols + diff --git a/tests/ruby/testcase/convertsymbolstostrings.rb b/tests/ruby/testcase/convertsymbolstostrings.rb new file mode 100644 index 0000000..b00c83c --- /dev/null +++ b/tests/ruby/testcase/convertsymbolstostrings.rb @@ -0,0 +1,2 @@ +world = :world +puts "hello #{world}" diff --git a/tests/ruby/testcase/globalprivatevariable.rb b/tests/ruby/testcase/globalprivatevariable.rb new file mode 100644 index 0000000..f0f63c0 --- /dev/null +++ b/tests/ruby/testcase/globalprivatevariable.rb @@ -0,0 +1,23 @@ +class Person + def initialize(name, age) + @name = name + @age = age + end + + public # This method can be called from outside the class. + + def about_me + puts "I'm #{@name} and I'm #{@age} years old!" + end + + private # This method can't! + + def bank_account_number + @account_number = 12345 + puts "My bank account number is #{@account_number}." + end +end + +eric = Person.new("Eric", 26) +eric.about_me +eric.bank_account_number \ No newline at end of file diff --git a/tests/ruby/testcase/hashhistogram.rb b/tests/ruby/testcase/hashhistogram.rb new file mode 100644 index 0000000..1c23a1b --- /dev/null +++ b/tests/ruby/testcase/hashhistogram.rb @@ -0,0 +1,9 @@ +puts "Text please: " +text = gets.chomp + +words = text.split(" ") +frequencies = Hash.new(0) +words.each { |word| frequencies[word] += 1 } +frequencies = frequencies.sort_by {|a, b| b } +frequencies.reverse! +frequencies.each { |word, frequency| puts word + " " + frequency.to_s } \ No newline at end of file diff --git a/tests/ruby/testcase/hashiteration.rb b/tests/ruby/testcase/hashiteration.rb new file mode 100644 index 0000000..e76d208 --- /dev/null +++ b/tests/ruby/testcase/hashiteration.rb @@ -0,0 +1,7 @@ +secret_identities = { "The Batman" => "Bruce Wayne", + "Superman" => "Clark Kent", + "Wonder Woman" => "Diana Prince", + "Freakazoid" => "Dexter Douglas" +} + +secret_identities.each { |name, ego| puts "#{name} " ' : ' " #{ego}" } \ No newline at end of file diff --git a/tests/ruby/testcase/hashlookupsymbolvsstring.rb b/tests/ruby/testcase/hashlookupsymbolvsstring.rb new file mode 100644 index 0000000..16498d3 --- /dev/null +++ b/tests/ruby/testcase/hashlookupsymbolvsstring.rb @@ -0,0 +1,15 @@ +require 'benchmark' + +string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)] +symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)] + +string_time = Benchmark.realtime do + 100_000.times { string_AZ["r"] } +end + +symbol_time = Benchmark.realtime do + 100_000.times { symbol_AZ[:r] } +end + +puts "String time: #{string_time} seconds." +puts "Symbol time: #{symbol_time} seconds." \ No newline at end of file diff --git a/tests/ruby/testcase/include.rb b/tests/ruby/testcase/include.rb new file mode 100644 index 0000000..475f786 --- /dev/null +++ b/tests/ruby/testcase/include.rb @@ -0,0 +1,15 @@ +class Angle + include Math + attr_accessor :radians + + def initialize(radians) + @radians = radians + end + + def cosine + cos(@radians) + end +end + +acute = Angle.new(1) +puts acute.cosine diff --git a/tests/ruby/testcase/lambdamethodsort.rb b/tests/ruby/testcase/lambdamethodsort.rb new file mode 100644 index 0000000..accc7e5 --- /dev/null +++ b/tests/ruby/testcase/lambdamethodsort.rb @@ -0,0 +1,13 @@ +crew = { + captain: "Picard", + first_officer: "Riker", + lt_cdr: "Data", + lt: "Worf", + ensign: "Ro", + counselor: "Troi", + chief_engineer: "LaForge", + doctor: "Crusher" +} +# Add your code below! +first_half = lambda{ |key, value| value < "M" } +puts crew.select(&first_half) diff --git a/tests/ruby/testcase/methodcapitalize.rb b/tests/ruby/testcase/methodcapitalize.rb new file mode 100644 index 0000000..31435eb --- /dev/null +++ b/tests/ruby/testcase/methodcapitalize.rb @@ -0,0 +1,10 @@ +# method that capitalizes a word +def capitalize(string) + puts "#{string[0].upcase}#{string[1..-1]}" +end + +capitalize("ryan") # prints "Ryan" +capitalize("jane") # prints "Jane" + +# block that capitalizes each string in the array +["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane" \ No newline at end of file diff --git a/tests/ruby/testcase/methodgreeter.rb b/tests/ruby/testcase/methodgreeter.rb new file mode 100644 index 0000000..f815480 --- /dev/null +++ b/tests/ruby/testcase/methodgreeter.rb @@ -0,0 +1,6 @@ +def greeter(name) + return print 'hello ' + name + ' nice to meet you!' +end +greeter ("jared") + + diff --git a/tests/ruby/testcase/methodprimenumbercalculator.rb b/tests/ruby/testcase/methodprimenumbercalculator.rb new file mode 100644 index 0000000..8323355 --- /dev/null +++ b/tests/ruby/testcase/methodprimenumbercalculator.rb @@ -0,0 +1,20 @@ +def prime(n) + puts "That's not an integer." unless n.is_a? Integer + is_prime = true + for i in 2..n-1 + if n % i == 0 + is_prime = false + end + end + if is_prime + puts "#{n} is prime!" + else + puts "#{n} is not prime." + end +end + +prime(2) +prime(9) +prime(11) +prime(51) +prime(97) \ No newline at end of file diff --git a/tests/ruby/testcase/modules.rb b/tests/ruby/testcase/modules.rb new file mode 100644 index 0000000..946db32 --- /dev/null +++ b/tests/ruby/testcase/modules.rb @@ -0,0 +1,28 @@ +module Action + def jump + @distance = rand(4) + 2 + puts "#{@name} jumped forward #{@distance} feet!" + end +end + +class Rabbit + include Action + attr_reader :name + def initialize(name) + @name = name + end +end + +class Cricket + include Action + attr_reader :name + def initialize(name) + @name = name + end +end + +peter = Rabbit.new("Peter") +jiminy = Cricket.new("Jiminy") + +peter.jump +jiminy.jump \ No newline at end of file diff --git a/tests/ruby/testcase/multipleinheritance.rb b/tests/ruby/testcase/multipleinheritance.rb new file mode 100644 index 0000000..867ab55 --- /dev/null +++ b/tests/ruby/testcase/multipleinheritance.rb @@ -0,0 +1,25 @@ +module Week + FIRST_DAY = "Sunday" + def Week.weeks_in_month + puts "You have four weeks in a month" + end + def Week.weeks_in_year + puts "You have 52 weeks in a year" + end +end + + +class Decade + include Week + no_of_yrs=10 + def no_of_months + puts Week::FIRST_DAY + number=10*12 + puts number + end +end +d1=Decade.new +puts Week::FIRST_DAY +Week.weeks_in_month +Week.weeks_in_year +d1.no_of_months diff --git a/tests/ruby/testcase/privatemethod.rb b/tests/ruby/testcase/privatemethod.rb new file mode 100644 index 0000000..68d1752 --- /dev/null +++ b/tests/ruby/testcase/privatemethod.rb @@ -0,0 +1,17 @@ +class Application + attr_accessor :status + def initialize; end +private +def password + @password = 12345 + end + + public + def print_status + puts "All systems go!" + end +end + +app = Application.new() +app.print_status() +app.password diff --git a/tests/ruby/testcase/procs.rb b/tests/ruby/testcase/procs.rb new file mode 100644 index 0000000..d43d155 --- /dev/null +++ b/tests/ruby/testcase/procs.rb @@ -0,0 +1,18 @@ +# Here at the amusement park, you have to be four feet tall +# or taller to ride the roller coaster. Let's use .select on +# each group to get only the ones four feet tall or taller. + +group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7] +group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0] +group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2] + + +over_4_feet = Proc.new { |x| x >= 4 } + + +print "group 1: #{group_1.select(&over_4_feet)}" +puts +print "group 2: #{group_2.select(&over_4_feet)}" +puts +print "group 3: #{group_3.select(&over_4_feet)}" + diff --git a/tests/ruby/testcase/selectivehash.rb b/tests/ruby/testcase/selectivehash.rb new file mode 100644 index 0000000..e3238b2 --- /dev/null +++ b/tests/ruby/testcase/selectivehash.rb @@ -0,0 +1,16 @@ +movie_ratings = { + memento: 3, + primer: 3.5, + the_matrix: 3, + truman_show: 4, + red_dawn: 1.5, + skyfall: 4, + alex_cross: 2, + uhf: 1, + lion_king: 3.5 +} +# Add your code below! + +good_movies = movie_ratings.select { |k, v| v > 3 } +print good_movies + diff --git a/tests/ruby/testcase/sortandorganizearray.rb b/tests/ruby/testcase/sortandorganizearray.rb new file mode 100644 index 0000000..c793ae8 --- /dev/null +++ b/tests/ruby/testcase/sortandorganizearray.rb @@ -0,0 +1,13 @@ +def alphabetize(arr, rev=false) + if rev + arr.sort { |item1, item2| item2 <=> item1 } + else + arr.sort { |item1, item2| item1 <=> item2 } + end +end + +books = ["Heart of Darkness", "Code Complete", "The Lorax", "The Prophet", "Absalom, Absalom!"] + +puts "A-Z: #{alphabetize(books)}" +puts "Z-A: #{alphabetize(books, true)}" + diff --git a/tests/ruby/testcase/soryarraybyalphabet.rb b/tests/ruby/testcase/soryarraybyalphabet.rb new file mode 100644 index 0000000..757977b --- /dev/null +++ b/tests/ruby/testcase/soryarraybyalphabet.rb @@ -0,0 +1,8 @@ +books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"] + +# To sort our books in ascending order, in-place +books.sort! { |firstBook, secondBook| firstBook <=> secondBook } + +# Sort your books in descending order, in-place below + +books.sort! { |secondBook, firstBook| firstBook <=> secondBook } \ No newline at end of file diff --git a/tests/ruby/testcase/stringinterpolation.rb b/tests/ruby/testcase/stringinterpolation.rb new file mode 100644 index 0000000..9db6aa1 --- /dev/null +++ b/tests/ruby/testcase/stringinterpolation.rb @@ -0,0 +1,7 @@ +favorite_things = ["Ruby", "espresso", "exercise"] + +puts "A few of my favorite things:" + +favorite_things.each do |thing| + puts "I love " "#{thing}!" +end \ No newline at end of file diff --git a/tests/ruby/testcase/substituion.rb b/tests/ruby/testcase/substituion.rb new file mode 100644 index 0000000..71bebf4 --- /dev/null +++ b/tests/ruby/testcase/substituion.rb @@ -0,0 +1,13 @@ +# Substituion Script + +print "Thtring, pleathe!: " +user_input = gets.chomp +user_input.downcase! + +if user_input.include? "s" + user_input.gsub!(/s/, "th") +else + puts "Nothing to do here!" +end + +puts "#{user_input}" diff --git a/tests/ruby/testcase/variablescope.rb b/tests/ruby/testcase/variablescope.rb new file mode 100644 index 0000000..65fdbaa --- /dev/null +++ b/tests/ruby/testcase/variablescope.rb @@ -0,0 +1,29 @@ +class Computer + $manufacturer = "Apple Computer, Inc." + @@files = {hello: "Hello, world!"} + + def initialize(username, password) + @username = username + @password = password + end + + def current_user + @username + end + + def self.display_files + @@files + end +end + +# Make a new Computer instance: +hal = Computer.new("Dave", 12345) + +puts "Current user: #{hal.current_user}" +# @username belongs to the hal instance. + +puts "Manufacturer: #{$manufacturer}" +# $manufacturer is global! We can get it directly. + +puts "Files: #{Computer.display_files}" +# @@files belongs to the Computer class. \ No newline at end of file diff --git a/tests/ruby/testcase/virtualcomputer.rb b/tests/ruby/testcase/virtualcomputer.rb new file mode 100644 index 0000000..1da3c6c --- /dev/null +++ b/tests/ruby/testcase/virtualcomputer.rb @@ -0,0 +1,23 @@ +class Computer + @@users = {} + def initialize(username, password) + @username = username + @password = password + @files = {} + @@users[username]=password +end + def create(filename) + time = Time.now + @files[filename]=time + puts "a file was made" +end + +def Computer.get_users + return @@users +end + +end + +my_computer = Computer.new("jared", "werba") + +puts Computer.get_users diff --git a/tests/ruby/testcase/yieldblocklambda.rb b/tests/ruby/testcase/yieldblocklambda.rb new file mode 100644 index 0000000..ca96e81 --- /dev/null +++ b/tests/ruby/testcase/yieldblocklambda.rb @@ -0,0 +1,11 @@ +def block_test + puts "We're in the method!" + puts "Yielding to the block..." + yield + yield + puts "We're back in the method!" +end + +block_test { puts ">>>>>>>>>> We're in the block!" } + +block_test { puts "below it?!?>>>>>>>>>>>>"} \ No newline at end of file diff --git a/tests/ruby/testcase/yieldingwithparameters.rb b/tests/ruby/testcase/yieldingwithparameters.rb new file mode 100644 index 0000000..952c197 --- /dev/null +++ b/tests/ruby/testcase/yieldingwithparameters.rb @@ -0,0 +1,10 @@ +def yield_name(name) + puts "In the method! Let's yield." + yield name + puts "Block complete! Back in the method." +end + +yield_name("Eric") { |name| puts "My name is #{name}." } + +# Now call the method with your name! +yield_name("jared"){|m| puts "ok"} \ No newline at end of file