mirror of
https://github.com/clearlinux/dockerfiles.git
synced 2026-10-03 23:38:31 +00:00
add ruby unit test
This commit is contained in:
Executable
+250
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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"
|
||||
@@ -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?!!?!?!!"})
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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}"
|
||||
@@ -0,0 +1,5 @@
|
||||
1.times do
|
||||
puts "I'm a code block!"
|
||||
end
|
||||
|
||||
5.times { puts "As am I!" }
|
||||
@@ -0,0 +1,5 @@
|
||||
abc = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
|
||||
|
||||
# Add your code below!
|
||||
|
||||
puts abc.collect{ |x| x + "!"}
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
world = :world
|
||||
puts "hello #{world}"
|
||||
@@ -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
|
||||
@@ -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 }
|
||||
@@ -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}" }
|
||||
@@ -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."
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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"
|
||||
@@ -0,0 +1,6 @@
|
||||
def greeter(name)
|
||||
return print 'hello ' + name + ' nice to meet you!'
|
||||
end
|
||||
greeter ("jared")
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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)}"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)}"
|
||||
|
||||
@@ -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 }
|
||||
@@ -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
|
||||
@@ -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}"
|
||||
@@ -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.
|
||||
@@ -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
|
||||
@@ -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?!?>>>>>>>>>>>>"}
|
||||
@@ -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"}
|
||||
Reference in New Issue
Block a user