mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-21 01:03:20 -04:00
Compare commits
1 Commits
rubykoans-
...
jruby_wip
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b97fea75a |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,3 @@
|
|||||||
dist
|
dist
|
||||||
.project_env.rc
|
.project_env.rc
|
||||||
.path_progress
|
.path_progress
|
||||||
*.rbc
|
|
||||||
|
|||||||
57
Rakefile
57
Rakefile
@@ -15,7 +15,6 @@ today = Time.now.strftime("%Y-%m-%d")
|
|||||||
TAR_FILE = "#{DIST_DIR}/rubykoans-#{today}.tgz"
|
TAR_FILE = "#{DIST_DIR}/rubykoans-#{today}.tgz"
|
||||||
ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip"
|
ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip"
|
||||||
|
|
||||||
CLEAN.include("**/*.rbc")
|
|
||||||
CLOBBER.include(DIST_DIR)
|
CLOBBER.include(DIST_DIR)
|
||||||
|
|
||||||
module Koans
|
module Koans
|
||||||
@@ -58,27 +57,6 @@ module Koans
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module RubyImpls
|
|
||||||
# Calculate the list of relevant Ruby implementations.
|
|
||||||
def self.find_ruby_impls
|
|
||||||
rubys = `rvm list`.gsub(/=>/,'').split(/\n/).sort
|
|
||||||
expected.map { |impl|
|
|
||||||
last = rubys.grep(Regexp.new(Regexp.quote(impl))).last
|
|
||||||
last ? last.split.first : nil
|
|
||||||
}.compact
|
|
||||||
end
|
|
||||||
|
|
||||||
# Return a (cached) list of relevant Ruby implementations.
|
|
||||||
def self.list
|
|
||||||
@list ||= find_ruby_impls
|
|
||||||
end
|
|
||||||
|
|
||||||
# List of expected ruby implementations.
|
|
||||||
def self.expected
|
|
||||||
%w(ruby-1.8.6 ruby-1.8.7 ruby-1.9.2 jruby ree)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
task :default => :walk_the_path
|
task :default => :walk_the_path
|
||||||
|
|
||||||
task :walk_the_path do
|
task :walk_the_path do
|
||||||
@@ -129,38 +107,3 @@ SRC_FILES.each do |koan_src|
|
|||||||
Koans.make_koan_file koan_src, t.name
|
Koans.make_koan_file koan_src, t.name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
task :run do
|
|
||||||
puts 'koans'
|
|
||||||
Dir.chdir("src") do
|
|
||||||
puts "in #{Dir.pwd}"
|
|
||||||
sh "ruby path_to_enlightenment.rb"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
desc "Pre-checkin tests (=> run_all)"
|
|
||||||
task :cruise => :run_all
|
|
||||||
|
|
||||||
desc "Run the completed koans againts a list of relevant Ruby Implementations"
|
|
||||||
task :run_all do
|
|
||||||
results = []
|
|
||||||
RubyImpls.list.each do |impl|
|
|
||||||
puts "=" * 40
|
|
||||||
puts "On Ruby #{impl}"
|
|
||||||
sh "rvm #{impl} rake run"
|
|
||||||
results << [impl, "RAN"]
|
|
||||||
puts
|
|
||||||
end
|
|
||||||
puts "=" * 40
|
|
||||||
puts "Summary:"
|
|
||||||
puts
|
|
||||||
results.each do |impl, res|
|
|
||||||
puts "#{impl} => RAN"
|
|
||||||
end
|
|
||||||
puts
|
|
||||||
RubyImpls.expected.each do |requested_impl|
|
|
||||||
impl_pattern = Regexp.new(Regexp.quote(requested_impl))
|
|
||||||
puts "No Results for #{requested_impl}" unless results.detect { |x| x.first =~ impl_pattern }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|||||||
@@ -18,19 +18,13 @@ class AboutArrayAssignment < EdgeCase::Koan
|
|||||||
assert_equal __, last_name
|
assert_equal __, last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parallel_assignments_with_splat_operator
|
def test_parallel_assignments_with_extra_variables
|
||||||
first_name, *last_name = ["John", "Smith", "III"]
|
|
||||||
assert_equal __, first_name
|
|
||||||
assert_equal __, last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_parallel_assignments_with_too_few_variables
|
|
||||||
first_name, last_name = ["Cher"]
|
first_name, last_name = ["Cher"]
|
||||||
assert_equal __, first_name
|
assert_equal __, first_name
|
||||||
assert_equal __, last_name
|
assert_equal __, last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parallel_assignments_with_subarrays
|
def test_parallel_assignements_with_subarrays
|
||||||
first_name, last_name = [["Willie", "Rae"], "Johnson"]
|
first_name, last_name = [["Willie", "Rae"], "Johnson"]
|
||||||
assert_equal __, first_name
|
assert_equal __, first_name
|
||||||
assert_equal __, last_name
|
assert_equal __, last_name
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ class AboutArrays < EdgeCase::Koan
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_arrays_and_ranges
|
def test_arrays_and_ranges
|
||||||
assert_equal __, (1..5).class
|
assert_equal Range, (1..5).class
|
||||||
assert_not_equal [1,2,3,4,5], (1..5)
|
assert_not_equal [1,2,3,4,5], (1..5)
|
||||||
assert_equal __, (1..5).to_a
|
assert_equal [1,2,3,4,5], (1..5).to_a
|
||||||
assert_equal __, (1...5).to_a
|
assert_equal __, (1...5).to_a
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ class AboutClasses < EdgeCase::Koan
|
|||||||
fido = Dog6.new("Fido")
|
fido = Dog6.new("Fido")
|
||||||
rover = Dog6.new("Rover")
|
rover = Dog6.new("Rover")
|
||||||
|
|
||||||
assert_equal __, rover.name != fido.name
|
assert_not_equal rover.name, fido.name
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@@ -164,12 +164,12 @@ class AboutClasses < EdgeCase::Koan
|
|||||||
|
|
||||||
def test_to_s_provides_a_string_version_of_the_object
|
def test_to_s_provides_a_string_version_of_the_object
|
||||||
fido = Dog7.new("Fido")
|
fido = Dog7.new("Fido")
|
||||||
assert_equal __, fido.to_s
|
assert_equal "Fido", fido.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_s_is_used_in_string_interpolation
|
def test_to_s_is_used_in_string_interpolation
|
||||||
fido = Dog7.new("Fido")
|
fido = Dog7.new("Fido")
|
||||||
assert_equal __, "My dog is #{fido}"
|
assert_equal "My dog is Fido", "My dog is #{fido}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_inspect_provides_a_more_complete_string_version
|
def test_inspect_provides_a_more_complete_string_version
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||||
|
|
||||||
# Implement a DiceSet Class here:
|
class DiceSet
|
||||||
#
|
attr_reader :values
|
||||||
# class DiceSet
|
def roll(n)
|
||||||
# code ...
|
@values = (1..n).map { rand(6) + 1 }
|
||||||
# end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class AboutDiceProject < EdgeCase::Koan
|
class AboutDiceProject < EdgeCase::Koan
|
||||||
def test_can_create_a_dice_set
|
def test_can_create_a_dice_set
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ class AboutExceptions < EdgeCase::Koan
|
|||||||
|
|
||||||
assert_equal __, result
|
assert_equal __, result
|
||||||
|
|
||||||
assert_equal __, ex.is_a?(StandardError), "Should be a Standard Error"
|
assert ex.is_a?(StandardError), "Failure message."
|
||||||
assert_equal __, ex.is_a?(RuntimeError), "Should be a Runtime Error"
|
assert ex.is_a?(RuntimeError), "Failure message."
|
||||||
|
|
||||||
assert RuntimeError.ancestors.include?(StandardError),
|
assert RuntimeError.ancestors.include?(StandardError),
|
||||||
"RuntimeError is a subclass of StandardError"
|
"RuntimeError is a subclass of StandardError"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
|||||||
class AboutHashes < EdgeCase::Koan
|
class AboutHashes < EdgeCase::Koan
|
||||||
def test_creating_hashes
|
def test_creating_hashes
|
||||||
empty_hash = Hash.new
|
empty_hash = Hash.new
|
||||||
assert_equal __, empty_hash.class
|
assert_equal Hash, empty_hash.class
|
||||||
assert_equal({}, empty_hash)
|
assert_equal({}, empty_hash)
|
||||||
assert_equal __, empty_hash.size
|
assert_equal __, empty_hash.size
|
||||||
end
|
end
|
||||||
@@ -25,7 +25,7 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
hash[:one] = "eins"
|
hash[:one] = "eins"
|
||||||
|
|
||||||
expected = { :one => __, :two => "dos" }
|
expected = { :one => __, :two => "dos" }
|
||||||
assert_equal __, expected == hash
|
assert_equal expected, hash
|
||||||
|
|
||||||
# Bonus Question: Why was "expected" broken out into a variable
|
# Bonus Question: Why was "expected" broken out into a variable
|
||||||
# rather than used as a literal?
|
# rather than used as a literal?
|
||||||
@@ -35,7 +35,7 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
hash1 = { :one => "uno", :two => "dos" }
|
hash1 = { :one => "uno", :two => "dos" }
|
||||||
hash2 = { :two => "dos", :one => "uno" }
|
hash2 = { :two => "dos", :one => "uno" }
|
||||||
|
|
||||||
assert_equal __, hash1 == hash2
|
assert_equal hash1, hash2
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hash_keys
|
def test_hash_keys
|
||||||
@@ -43,7 +43,7 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
assert_equal __, hash.keys.size
|
assert_equal __, hash.keys.size
|
||||||
assert_equal __, hash.keys.include?(:one)
|
assert_equal __, hash.keys.include?(:one)
|
||||||
assert_equal __, hash.keys.include?(:two)
|
assert_equal __, hash.keys.include?(:two)
|
||||||
assert_equal __, hash.keys.class
|
assert_equal Array, hash.keys.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hash_values
|
def test_hash_values
|
||||||
@@ -51,16 +51,16 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
assert_equal __, hash.values.size
|
assert_equal __, hash.values.size
|
||||||
assert_equal __, hash.values.include?("uno")
|
assert_equal __, hash.values.include?("uno")
|
||||||
assert_equal __, hash.values.include?("dos")
|
assert_equal __, hash.values.include?("dos")
|
||||||
assert_equal __, hash.values.class
|
assert_equal Array, hash.values.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_combining_hashes
|
def test_combining_hashes
|
||||||
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
|
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
|
||||||
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })
|
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })
|
||||||
|
|
||||||
assert_equal __, hash != new_hash
|
assert_not_equal hash, new_hash
|
||||||
|
|
||||||
expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
|
expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
|
||||||
assert_equal __, expected == new_hash
|
assert_equal expected, new_hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class AboutIteration < EdgeCase::Koan
|
|||||||
array.each do |item|
|
array.each do |item|
|
||||||
sum += item
|
sum += item
|
||||||
end
|
end
|
||||||
assert_equal __, sum
|
assert_equal 6, sum
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_each_can_use_curly_brace_blocks_too
|
def test_each_can_use_curly_brace_blocks_too
|
||||||
|
|||||||
@@ -1,137 +0,0 @@
|
|||||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
|
||||||
|
|
||||||
include Java
|
|
||||||
|
|
||||||
# Concepts
|
|
||||||
# * Pull in a java class
|
|
||||||
# * calling a method, Camel vs snake
|
|
||||||
# * Resovling module/class name conflicts
|
|
||||||
# * Showing what gets returned
|
|
||||||
# * Ruby Strings VS Java Strings
|
|
||||||
# * Calling custom java class
|
|
||||||
# * Calling Ruby from java???
|
|
||||||
|
|
||||||
class AboutJavaInterop < EdgeCase::Koan
|
|
||||||
def test_using_a_java_library_class
|
|
||||||
java_array = java.util.ArrayList.new
|
|
||||||
assert_equal __, java_array.class
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_class_can_be_referenced_using_both_ruby_and_java_like_syntax
|
|
||||||
assert_equal __, Java::JavaUtil::ArrayList == java.util.ArrayList
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_include_class_includes_class_in_module_scope
|
|
||||||
assert_nil defined?(TreeSet)
|
|
||||||
include_class "java.util.TreeSet"
|
|
||||||
assert_equal __, defined?(TreeSet)
|
|
||||||
end
|
|
||||||
|
|
||||||
# THINK ABOUT IT:
|
|
||||||
#
|
|
||||||
# What if we use:
|
|
||||||
#
|
|
||||||
# include_class "java.lang.String"
|
|
||||||
#
|
|
||||||
# What would be the value of the String constant after this
|
|
||||||
# include_class is run? Would it be useful to provide a way of
|
|
||||||
# aliasing java classes to different names?
|
|
||||||
|
|
||||||
JString = java.lang.String
|
|
||||||
def test_also_java_class_can_be_given_ruby_aliases
|
|
||||||
java_string = JString.new("A Java String")
|
|
||||||
assert_equal __, java_string.class
|
|
||||||
assert_equal __, JString
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_can_directly_call_java_methods_on_java_objects
|
|
||||||
java_string = JString.new("A Java String")
|
|
||||||
assert_equal __, java_string.toLowerCase
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_jruby_provides_snake_case_versions_of_java_methods
|
|
||||||
java_string = JString.new("A Java String")
|
|
||||||
assert_equal __, java_string.to_lower_case
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_jruby_provides_question_mark_versions_of_boolean_methods
|
|
||||||
java_string = JString.new("A Java String")
|
|
||||||
assert_equal __, java_string.endsWith("String")
|
|
||||||
assert_equal __, java_string.ends_with("String")
|
|
||||||
assert_equal __, java_string.ends_with?("String")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_string_are_not_ruby_strings
|
|
||||||
ruby_string = "A Java String"
|
|
||||||
java_string = java.lang.String.new(ruby_string)
|
|
||||||
assert_equal __, java_string.is_a?(java.lang.String)
|
|
||||||
assert_equal __, java_string.is_a?(String)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_strings_can_be_compared_to_ruby_strings_maybe
|
|
||||||
ruby_string = "A Java String"
|
|
||||||
java_string = java.lang.String.new(ruby_string)
|
|
||||||
assert_equal __, ruby_string == java_string
|
|
||||||
assert_equal __, java_string == ruby_string
|
|
||||||
|
|
||||||
# THINK ABOUT IT:
|
|
||||||
#
|
|
||||||
# Is there any possible way for this to be more wrong?
|
|
||||||
#
|
|
||||||
# SERIOUSLY, THINK ABOUT IT:
|
|
||||||
#
|
|
||||||
# Why do you suppose that Ruby and Java strings compare like that?
|
|
||||||
#
|
|
||||||
# ADVANCED THINK ABOUT IT:
|
|
||||||
#
|
|
||||||
# Is there a way to make Ruby/Java string comparisons commutative?
|
|
||||||
# How would you do it?
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_however_most_methods_returning_strings_return_ruby_strings
|
|
||||||
java_array = java.util.ArrayList.new
|
|
||||||
assert_equal __, java_array.toString
|
|
||||||
assert_equal __, java_array.toString.is_a?(String)
|
|
||||||
assert_equal __, java_array.toString.is_a?(java.lang.String)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_some_ruby_objects_can_be_coerced_to_java
|
|
||||||
assert_equal __, "ruby string".to_java.class
|
|
||||||
assert_equal __, 1.to_java.class
|
|
||||||
assert_equal __, 9.32.to_java.class
|
|
||||||
assert_equal __, false.to_java.class
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_some_ruby_objects_are_not_coerced_to_what_you_might_expect
|
|
||||||
assert_equal __, [].to_java.class == Java::JavaUtil::ArrayList
|
|
||||||
assert_equal __, {}.to_java.class == Java::JavaUtil::HashMap
|
|
||||||
assert_equal __, Object.new.to_java.class == Java::JavaLang::Object
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_collections_are_enumerable
|
|
||||||
java_array = java.util.ArrayList.new
|
|
||||||
java_array << "one" << "two" << "three"
|
|
||||||
assert_equal __, java_array.map { |item| item.upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
# Open the Java ArrayList class and add a new method.
|
|
||||||
class Java::JavaUtil::ArrayList
|
|
||||||
def multiply_all
|
|
||||||
result = 1
|
|
||||||
each do |item|
|
|
||||||
result *= item
|
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_class_are_open_from_ruby
|
|
||||||
java_array = java.util.ArrayList.new
|
|
||||||
java_array.add_all([1,2,3,4,5])
|
|
||||||
|
|
||||||
assert_equal __, java_array.multiply_all
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -36,12 +36,12 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
exception = assert_raise(___) do
|
exception = assert_raise(___) do
|
||||||
my_global_method
|
my_global_method
|
||||||
end
|
end
|
||||||
assert_match(/__/, exception.message)
|
assert_match(/#{__ of arguments")}/, exception.message)
|
||||||
|
|
||||||
exception = assert_raise(___) do
|
exception = assert_raise(___) do
|
||||||
my_global_method(1,2,3)
|
my_global_method(1,2,3)
|
||||||
end
|
end
|
||||||
assert_match(/__/, exception.message)
|
assert_match(/#{__ of arguments")}/, exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
|||||||
|
|
||||||
class AboutRegularExpressions < EdgeCase::Koan
|
class AboutRegularExpressions < EdgeCase::Koan
|
||||||
def test_a_pattern_is_a_regular_expression
|
def test_a_pattern_is_a_regular_expression
|
||||||
assert_equal __, /pattern/.class
|
assert_equal Regexp, /pattern/.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_a_regexp_can_search_a_string_for_matching_content
|
def test_a_regexp_can_search_a_string_for_matching_content
|
||||||
assert_equal __, "some matching content"[/match/]
|
assert_equal "match", "some matching content"[/match/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_a_failed_match_returns_nil
|
def test_a_failed_match_returns_nil
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ class AboutScope < EdgeCase::Koan
|
|||||||
assert_equal __, fido.identify
|
assert_equal __, fido.identify
|
||||||
assert_equal __, rover.identify
|
assert_equal __, rover.identify
|
||||||
|
|
||||||
assert_equal __, fido.class != rover.class
|
assert_not_equal fido.class, rover.class
|
||||||
assert_equal __, Jims::Dog != Joes::Dog
|
assert_not_equal Jims::Dog, Joes::Dog
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
symbol2 = :a_symbol
|
symbol2 = :a_symbol
|
||||||
symbol3 = :something_else
|
symbol3 = :something_else
|
||||||
|
|
||||||
assert_equal __, symbol1 == symbol2
|
assert symbol1 == __
|
||||||
assert_equal __, symbol1 == symbol3
|
assert symbol1 != __
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_identical_symbols_are_a_single_internal_object
|
def test_identical_symbols_are_a_single_internal_object
|
||||||
@@ -24,14 +24,14 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_method_names_become_symbols
|
def test_method_names_become_symbols
|
||||||
symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
|
all_symbols = Symbol.all_symbols
|
||||||
assert_equal __, symbols_as_strings.include?("test_method_names_become_symbols")
|
assert_equal __, all_symbols.include?(:test_method_names_become_symbols)
|
||||||
end
|
end
|
||||||
|
|
||||||
# THINK ABOUT IT:
|
# THINK ABOUT IT:
|
||||||
#
|
#
|
||||||
# Why do we convert the list of symbols to strings and then compare
|
# Why do we capture the list of symbols before we check for the
|
||||||
# against the string value rather than against symbols?
|
# method name?
|
||||||
|
|
||||||
in_ruby_version("mri") do
|
in_ruby_version("mri") do
|
||||||
RubyConstant = "What is the sound of one hand clapping?"
|
RubyConstant = "What is the sound of one hand clapping?"
|
||||||
@@ -53,7 +53,7 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal symbol, __.to_sym
|
assert_equal symbol, __.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_symbols_with_interpolation_can_be_built
|
def test_symbols_with_spaces_can_be_built
|
||||||
value = "and"
|
value = "and"
|
||||||
symbol = :"cats #{value} dogs"
|
symbol = :"cats #{value} dogs"
|
||||||
|
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
|
||||||
|
|
||||||
class AboutToStr < EdgeCase::Koan
|
|
||||||
|
|
||||||
class CanNotBeTreatedAsString
|
|
||||||
def to_s
|
|
||||||
"non-string-like"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_s_returns_a_string_representation
|
|
||||||
not_like_a_string = CanNotBeTreatedAsString.new
|
|
||||||
assert_equal __, not_like_a_string.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_normally_objects_cannot_be_used_where_strings_are_expected
|
|
||||||
assert_raise(___) do
|
|
||||||
File.exist?(CanNotBeTreatedAsString.new)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
class CanBeTreatedAsString
|
|
||||||
def to_s
|
|
||||||
"string-like"
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_str
|
|
||||||
to_s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_str_also_returns_a_string_representation
|
|
||||||
like_a_string = CanBeTreatedAsString.new
|
|
||||||
assert_equal __, like_a_string.to_str
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_str_allows_objects_to_be_treated_as_strings
|
|
||||||
assert_equal __, File.exist?(CanBeTreatedAsString.new)
|
|
||||||
end
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
def acts_like_a_string?(string)
|
|
||||||
string = string.to_str if string.respond_to?(:to_str)
|
|
||||||
string.is_a?(String)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_user_defined_code_can_check_for_to_str
|
|
||||||
assert_equal __, acts_like_a_string?(CanNotBeTreatedAsString.new)
|
|
||||||
assert_equal __, acts_like_a_string?(CanBeTreatedAsString.new)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -3,10 +3,6 @@
|
|||||||
|
|
||||||
require 'test/unit/assertions'
|
require 'test/unit/assertions'
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
|
||||||
# Support code for the Ruby Koans.
|
|
||||||
# --------------------------------------------------------------------
|
|
||||||
|
|
||||||
class FillMeInError < StandardError
|
class FillMeInError < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -20,8 +16,6 @@ def in_ruby_version(*versions)
|
|||||||
yield if versions.any? { |v| ruby_version?(v) }
|
yield if versions.any? { |v| ruby_version?(v) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Standard, generic replacement value.
|
|
||||||
# If value19 is given, it is used inplace of value for Ruby 1.9.
|
|
||||||
def __(value="FILL ME IN", value19=:mu)
|
def __(value="FILL ME IN", value19=:mu)
|
||||||
if RUBY_VERSION < "1.9"
|
if RUBY_VERSION < "1.9"
|
||||||
value
|
value
|
||||||
@@ -30,7 +24,6 @@ def __(value="FILL ME IN", value19=:mu)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Numeric replacement value.
|
|
||||||
def _n_(value=999999, value19=:mu)
|
def _n_(value=999999, value19=:mu)
|
||||||
if RUBY_VERSION < "1.9"
|
if RUBY_VERSION < "1.9"
|
||||||
value
|
value
|
||||||
@@ -39,12 +32,10 @@ def _n_(value=999999, value19=:mu)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Error object replacement value.
|
|
||||||
def ___(value=FillMeInError)
|
def ___(value=FillMeInError)
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
# Method name replacement.
|
|
||||||
class Object
|
class Object
|
||||||
def ____(method=nil)
|
def ____(method=nil)
|
||||||
if method
|
if method
|
||||||
@@ -57,25 +48,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class String
|
|
||||||
def side_padding(width)
|
|
||||||
extra = width - self.size
|
|
||||||
if width < 0
|
|
||||||
self
|
|
||||||
else
|
|
||||||
left_padding = extra / 2
|
|
||||||
right_padding = (extra+1)/2
|
|
||||||
(" " * left_padding) + self + (" " *right_padding)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module EdgeCase
|
module EdgeCase
|
||||||
class << self
|
|
||||||
def simple_output
|
|
||||||
ENV['SIMPLE_KOAN_OUTPUT'] == 'true'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module Color
|
module Color
|
||||||
#shamelessly stolen (and modified) from redgreen
|
#shamelessly stolen (and modified) from redgreen
|
||||||
@@ -93,29 +66,16 @@ module EdgeCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def colorize(string, color_value)
|
def colorize(string, color_value)
|
||||||
if use_colors?
|
if ENV['NO_COLOR']
|
||||||
color(color_value) + string + color(COLORS[:clear])
|
|
||||||
else
|
|
||||||
string
|
string
|
||||||
|
else
|
||||||
|
color(color_value) + string + color(COLORS[:clear])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def color(color_value)
|
def color(color_value)
|
||||||
"\e[#{color_value}m"
|
"\e[#{color_value}m"
|
||||||
end
|
end
|
||||||
|
|
||||||
def use_colors?
|
|
||||||
return false if ENV['NO_COLOR']
|
|
||||||
if ENV['ANSI_COLOR'].nil?
|
|
||||||
! using_windows?
|
|
||||||
else
|
|
||||||
ENV['ANSI_COLOR'] =~ /^(t|y)/i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def using_windows?
|
|
||||||
File::ALT_SEPARATOR
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Sensei
|
class Sensei
|
||||||
@@ -216,21 +176,6 @@ module EdgeCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def end_screen
|
def end_screen
|
||||||
if EdgeCase.simple_output
|
|
||||||
boring_end_screen
|
|
||||||
else
|
|
||||||
artistic_end_screen
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def boring_end_screen
|
|
||||||
puts "Mountains are again merely mountains"
|
|
||||||
end
|
|
||||||
|
|
||||||
def artistic_end_screen
|
|
||||||
"JRuby 1.9.x Koans"
|
|
||||||
ruby_version = "(in #{'J' if defined?(JRUBY_VERSION)}Ruby #{defined?(JRUBY_VERSION) ? JRUBY_VERSION : RUBY_VERSION})"
|
|
||||||
ruby_version = ruby_version.side_padding(54)
|
|
||||||
completed = <<-ENDTEXT
|
completed = <<-ENDTEXT
|
||||||
,, , ,,
|
,, , ,,
|
||||||
: ::::, :::,
|
: ::::, :::,
|
||||||
@@ -247,8 +192,8 @@ module EdgeCase
|
|||||||
,:::::::::::, ::::::::::::,
|
,:::::::::::, ::::::::::::,
|
||||||
:::::::::::, ,::::::::::::
|
:::::::::::, ,::::::::::::
|
||||||
::::::::::::: ,::::::::::::
|
::::::::::::: ,::::::::::::
|
||||||
:::::::::::: Ruby Koans ::::::::::::,
|
:::::::::::: Ruby Koans ::::::::::::,
|
||||||
::::::::::::#{ ruby_version },::::::::::::,
|
:::::::::::: ,::::::::::::,
|
||||||
:::::::::::, , ::::::::::::
|
:::::::::::, , ::::::::::::
|
||||||
,:::::::::::::, brought to you by ,,::::::::::::,
|
,:::::::::::::, brought to you by ,,::::::::::::,
|
||||||
:::::::::::::: ,::::::::::::
|
:::::::::::::: ,::::::::::::
|
||||||
@@ -315,7 +260,7 @@ ENDTEXT
|
|||||||
|
|
||||||
def find_interesting_lines(backtrace)
|
def find_interesting_lines(backtrace)
|
||||||
backtrace.reject { |line|
|
backtrace.reject { |line|
|
||||||
line =~ /test\/unit\/|edgecase\.rb|minitest/
|
line =~ /test\/unit\/|edgecase\.rb/
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -31,8 +31,4 @@ require 'about_scope'
|
|||||||
require 'about_class_methods'
|
require 'about_class_methods'
|
||||||
require 'about_message_passing'
|
require 'about_message_passing'
|
||||||
require 'about_proxy_object_project'
|
require 'about_proxy_object_project'
|
||||||
require 'about_to_str'
|
|
||||||
in_ruby_version("jruby") do
|
|
||||||
require 'about_java_interop'
|
|
||||||
end
|
|
||||||
require 'about_extra_credit'
|
require 'about_extra_credit'
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ namespace "check" do
|
|||||||
puts
|
puts
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Check that asserts have __ replacements"
|
|
||||||
task :asserts do
|
task :asserts do
|
||||||
puts "Checking for asserts missing the replacement text:"
|
puts "Checking for asserts missing the replacement text:"
|
||||||
begin
|
begin
|
||||||
@@ -29,5 +28,4 @@ namespace "check" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Run some simple consistancy checks"
|
|
||||||
task :check => ["check:abouts", "check:asserts"]
|
task :check => ["check:abouts", "check:asserts"]
|
||||||
|
|||||||
@@ -10,3 +10,7 @@ task :test do
|
|||||||
ruby 'path_to_enlightenment.rb'
|
ruby 'path_to_enlightenment.rb'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
task :java do
|
||||||
|
`javac **/*.java`
|
||||||
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,19 +18,13 @@ class AboutArrayAssignment < EdgeCase::Koan
|
|||||||
assert_equal __("Smith"), last_name
|
assert_equal __("Smith"), last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parallel_assignments_with_splat_operator
|
def test_parallel_assignments_with_extra_variables
|
||||||
first_name, *last_name = ["John", "Smith", "III"]
|
|
||||||
assert_equal __("John"), first_name
|
|
||||||
assert_equal __(["Smith","III"]), last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_parallel_assignments_with_too_few_variables
|
|
||||||
first_name, last_name = ["Cher"]
|
first_name, last_name = ["Cher"]
|
||||||
assert_equal __("Cher"), first_name
|
assert_equal __("Cher"), first_name
|
||||||
assert_equal __(nil), last_name
|
assert_equal __(nil), last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parallel_assignments_with_subarrays
|
def test_parallel_assignements_with_subarrays
|
||||||
first_name, last_name = [["Willie", "Rae"], "Johnson"]
|
first_name, last_name = [["Willie", "Rae"], "Johnson"]
|
||||||
assert_equal __(["Willie", "Rae"]), first_name
|
assert_equal __(["Willie", "Rae"]), first_name
|
||||||
assert_equal __("Johnson"), last_name
|
assert_equal __("Johnson"), last_name
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||||
|
|
||||||
# Implement a DiceSet Class here:
|
|
||||||
#
|
|
||||||
# class DiceSet
|
|
||||||
# code ...
|
|
||||||
# end
|
|
||||||
|
|
||||||
#--
|
|
||||||
class DiceSet
|
class DiceSet
|
||||||
attr_reader :values
|
attr_reader :values
|
||||||
def roll(n)
|
def roll(n)
|
||||||
@@ -14,7 +7,6 @@ class DiceSet
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#++
|
|
||||||
class AboutDiceProject < EdgeCase::Koan
|
class AboutDiceProject < EdgeCase::Koan
|
||||||
def test_can_create_a_dice_set
|
def test_can_create_a_dice_set
|
||||||
dice = DiceSet.new
|
dice = DiceSet.new
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ class AboutExceptions < EdgeCase::Koan
|
|||||||
|
|
||||||
assert_equal __(:exception_handled), result
|
assert_equal __(:exception_handled), result
|
||||||
|
|
||||||
assert_equal __(true), ex.is_a?(StandardError), "Should be a Standard Error"
|
assert ex.is_a?(___(StandardError)), "Failure message."
|
||||||
assert_equal __(true), ex.is_a?(RuntimeError), "Should be a Runtime Error"
|
assert ex.is_a?(___(RuntimeError)), "Failure message."
|
||||||
|
|
||||||
assert RuntimeError.ancestors.include?(StandardError), # __
|
assert RuntimeError.ancestors.include?(StandardError), # __
|
||||||
"RuntimeError is a subclass of StandardError"
|
"RuntimeError is a subclass of StandardError"
|
||||||
|
|||||||
@@ -75,17 +75,7 @@ class AboutJavaInterop < EdgeCase::Koan
|
|||||||
assert_equal __(true), java_string == ruby_string
|
assert_equal __(true), java_string == ruby_string
|
||||||
|
|
||||||
# THINK ABOUT IT:
|
# THINK ABOUT IT:
|
||||||
#
|
|
||||||
# Is there any possible way for this to be more wrong?
|
# Is there any possible way for this to be more wrong?
|
||||||
#
|
|
||||||
# SERIOUSLY, THINK ABOUT IT:
|
|
||||||
#
|
|
||||||
# Why do you suppose that Ruby and Java strings compare like that?
|
|
||||||
#
|
|
||||||
# ADVANCED THINK ABOUT IT:
|
|
||||||
#
|
|
||||||
# Is there a way to make Ruby/Java string comparisons commutative?
|
|
||||||
# How would you do it?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_however_most_methods_returning_strings_return_ruby_strings
|
def test_however_most_methods_returning_strings_return_ruby_strings
|
||||||
@@ -95,43 +85,15 @@ class AboutJavaInterop < EdgeCase::Koan
|
|||||||
assert_equal __(false), java_array.toString.is_a?(java.lang.String)
|
assert_equal __(false), java_array.toString.is_a?(java.lang.String)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_some_ruby_objects_can_be_coerced_to_java
|
def test_call_custom_class
|
||||||
assert_equal __(Java::JavaLang::String), "ruby string".to_java.class
|
number_generator = com.edgecase.JavaStuff.new
|
||||||
assert_equal __(Java::JavaLang::Long), 1.to_java.class
|
assert_equal __(true), number_generator.random_number > 0
|
||||||
assert_equal __(Java::JavaLang::Double), 9.32.to_java.class
|
|
||||||
assert_equal __(Java::JavaLang::Boolean), false.to_java.class
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_some_ruby_objects_are_not_coerced_to_what_you_might_expect
|
|
||||||
assert_equal __(false), [].to_java.class == Java::JavaUtil::ArrayList
|
|
||||||
assert_equal __(false), {}.to_java.class == Java::JavaUtil::HashMap
|
|
||||||
assert_equal __(false), Object.new.to_java.class == Java::JavaLang::Object
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_collections_are_enumerable
|
|
||||||
java_array = java.util.ArrayList.new
|
|
||||||
java_array << "one" << "two" << "three"
|
|
||||||
assert_equal __(["ONE", "TWO", "THREE"]), java_array.map { |item| item.upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
# Open the Java ArrayList class and add a new method.
|
|
||||||
class Java::JavaUtil::ArrayList
|
|
||||||
def multiply_all
|
|
||||||
result = 1
|
|
||||||
each do |item|
|
|
||||||
result *= item
|
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_java_class_are_open_from_ruby
|
|
||||||
java_array = java.util.ArrayList.new
|
|
||||||
java_array.add_all([1,2,3,4,5])
|
|
||||||
|
|
||||||
assert_equal __(120), java_array.multiply_all
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# FREAKS OUT
|
||||||
|
# def test_call_ruby_to_java_to_ruby
|
||||||
|
# number_generator = com.edgecase.JavaToRuby.new
|
||||||
|
# puts number_generator.random_number
|
||||||
|
# puts "*"*20
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -43,15 +43,12 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
exception = assert_raise(___(ArgumentError)) do
|
exception = assert_raise(___(ArgumentError)) do
|
||||||
my_global_method
|
my_global_method
|
||||||
end
|
end
|
||||||
#--
|
assert_match(/#{__("wrong (number|#) of arguments")}/, exception.message)
|
||||||
pattern = "wrong (number|#) of arguments"
|
|
||||||
#++
|
|
||||||
assert_match(/#{__(pattern)}/, exception.message)
|
|
||||||
|
|
||||||
exception = assert_raise(___(ArgumentError)) do
|
exception = assert_raise(___(ArgumentError)) do
|
||||||
my_global_method(1,2,3)
|
my_global_method(1,2,3)
|
||||||
end
|
end
|
||||||
assert_match(/#{__(pattern)}/, exception.message)
|
assert_match(/#{__("wrong (number|#) of arguments")}/, exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
symbol2 = :a_symbol
|
symbol2 = :a_symbol
|
||||||
symbol3 = :something_else
|
symbol3 = :something_else
|
||||||
|
|
||||||
assert_equal __(true), symbol1 == symbol2
|
assert symbol1 == __(symbol2)
|
||||||
assert_equal __(false), symbol1 == symbol3
|
assert symbol1 != __(symbol3)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_identical_symbols_are_a_single_internal_object
|
def test_identical_symbols_are_a_single_internal_object
|
||||||
@@ -24,14 +24,14 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_method_names_become_symbols
|
def test_method_names_become_symbols
|
||||||
symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
|
all_symbols = Symbol.all_symbols
|
||||||
assert_equal __(true), symbols_as_strings.include?("test_method_names_become_symbols")
|
assert_equal __(true), all_symbols.include?(:test_method_names_become_symbols)
|
||||||
end
|
end
|
||||||
|
|
||||||
# THINK ABOUT IT:
|
# THINK ABOUT IT:
|
||||||
#
|
#
|
||||||
# Why do we convert the list of symbols to strings and then compare
|
# Why do we capture the list of symbols before we check for the
|
||||||
# against the string value rather than against symbols?
|
# method name?
|
||||||
|
|
||||||
in_ruby_version("mri") do
|
in_ruby_version("mri") do
|
||||||
RubyConstant = "What is the sound of one hand clapping?"
|
RubyConstant = "What is the sound of one hand clapping?"
|
||||||
@@ -53,7 +53,7 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal symbol, __("cats and dogs").to_sym
|
assert_equal symbol, __("cats and dogs").to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_symbols_with_interpolation_can_be_built
|
def test_symbols_with_spaces_can_be_built
|
||||||
value = "and"
|
value = "and"
|
||||||
symbol = :"cats #{value} dogs"
|
symbol = :"cats #{value} dogs"
|
||||||
|
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
|
||||||
|
|
||||||
class AboutToStr < EdgeCase::Koan
|
|
||||||
|
|
||||||
class CanNotBeTreatedAsString
|
|
||||||
def to_s
|
|
||||||
"non-string-like"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_s_returns_a_string_representation
|
|
||||||
not_like_a_string = CanNotBeTreatedAsString.new
|
|
||||||
assert_equal __("non-string-like"), not_like_a_string.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_normally_objects_cannot_be_used_where_strings_are_expected
|
|
||||||
assert_raise(___(TypeError)) do
|
|
||||||
File.exist?(CanNotBeTreatedAsString.new)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
class CanBeTreatedAsString
|
|
||||||
def to_s
|
|
||||||
"string-like"
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_str
|
|
||||||
to_s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_str_also_returns_a_string_representation
|
|
||||||
like_a_string = CanBeTreatedAsString.new
|
|
||||||
assert_equal __("string-like"), like_a_string.to_str
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_str_allows_objects_to_be_treated_as_strings
|
|
||||||
assert_equal __(false), File.exist?(CanBeTreatedAsString.new)
|
|
||||||
end
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
def acts_like_a_string?(string)
|
|
||||||
string = string.to_str if string.respond_to?(:to_str)
|
|
||||||
string.is_a?(String)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_user_defined_code_can_check_for_to_str
|
|
||||||
assert_equal __(false), acts_like_a_string?(CanNotBeTreatedAsString.new)
|
|
||||||
assert_equal __(true), acts_like_a_string?(CanBeTreatedAsString.new)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
BIN
src/com/edgecase/JavaStuff.class
Normal file
BIN
src/com/edgecase/JavaStuff.class
Normal file
Binary file not shown.
18
src/com/edgecase/JavaStuff.java
Normal file
18
src/com/edgecase/JavaStuff.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.edgecase;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
class JavaStuff{
|
||||||
|
public static final Random GENERATOR = new Random(System.currentTimeMillis());
|
||||||
|
public static final int DEFAULT_MAX = 100;
|
||||||
|
|
||||||
|
public JavaStuff(){}
|
||||||
|
|
||||||
|
public int randomNumber(){
|
||||||
|
return randomNumber(DEFAULT_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int randomNumber(int max){
|
||||||
|
return GENERATOR.nextInt(max) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/com/edgecase/JavaToRuby.class
Normal file
BIN
src/com/edgecase/JavaToRuby.class
Normal file
Binary file not shown.
28
src/com/edgecase/JavaToRuby.java
Normal file
28
src/com/edgecase/JavaToRuby.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package com.edgecase;
|
||||||
|
|
||||||
|
import org.jruby.Ruby;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
|
|
||||||
|
class JavaToRuby{
|
||||||
|
public static final Ruby RUBY_RUNTIME = Ruby.getDefaultInstance();
|
||||||
|
public static final int DEFAULT_MAX = 100;
|
||||||
|
|
||||||
|
public JavaToRuby() {}
|
||||||
|
|
||||||
|
public Object randomNumber() throws ScriptException{
|
||||||
|
return randomNumber(DEFAULT_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object randomNumber(int max) throws ScriptException{
|
||||||
|
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
|
||||||
|
/* jruby.put("message", "hello world"); */
|
||||||
|
return jruby.eval("puts 'craaaaaaaap!!!!'; rand "+max);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -93,29 +93,16 @@ module EdgeCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def colorize(string, color_value)
|
def colorize(string, color_value)
|
||||||
if use_colors?
|
if ENV['NO_COLOR']
|
||||||
color(color_value) + string + color(COLORS[:clear])
|
|
||||||
else
|
|
||||||
string
|
string
|
||||||
|
else
|
||||||
|
color(color_value) + string + color(COLORS[:clear])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def color(color_value)
|
def color(color_value)
|
||||||
"\e[#{color_value}m"
|
"\e[#{color_value}m"
|
||||||
end
|
end
|
||||||
|
|
||||||
def use_colors?
|
|
||||||
return false if ENV['NO_COLOR']
|
|
||||||
if ENV['ANSI_COLOR'].nil?
|
|
||||||
! using_windows?
|
|
||||||
else
|
|
||||||
ENV['ANSI_COLOR'] =~ /^(t|y)/i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def using_windows?
|
|
||||||
File::ALT_SEPARATOR
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Sensei
|
class Sensei
|
||||||
|
|||||||
@@ -31,8 +31,4 @@ require 'about_scope'
|
|||||||
require 'about_class_methods'
|
require 'about_class_methods'
|
||||||
require 'about_message_passing'
|
require 'about_message_passing'
|
||||||
require 'about_proxy_object_project'
|
require 'about_proxy_object_project'
|
||||||
require 'about_to_str'
|
|
||||||
in_ruby_version("jruby") do
|
|
||||||
require 'about_java_interop'
|
|
||||||
end
|
|
||||||
require 'about_extra_credit'
|
require 'about_extra_credit'
|
||||||
|
|||||||
5
src/try_java_to_ruby.rb
Normal file
5
src/try_java_to_ruby.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
include Java
|
||||||
|
|
||||||
|
number_generator = com.edgecase.JavaToRuby.new
|
||||||
|
puts number_generator.random_number
|
||||||
|
puts "*"*20
|
||||||
Reference in New Issue
Block a user