Created
March 14, 2011 15:57
-
-
Save marshluca/869366 to your computer and use it in GitHub Desktop.
monday.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# object_model/alphanumeric.rb | |
require "test/unit" | |
class String | |
def to_alphanumeric | |
gsub(/[^\w\s]/,'') | |
end | |
end | |
class ToAlphanumericTest < Test::Unit::TestCase | |
def test_strips_non_alphanumeric_characters | |
assert_equal '3 the Magic Number', '3#, the *Magic, Number*?'.to_alphanumeric | |
end | |
end | |
# object_model/replace.rb | |
require "test/unit" | |
class Array | |
def substitute(from, to) | |
each_with_index { |e, i| self[i] = to if e == from } | |
end | |
end | |
class ReplaceTest < Test::Unit::TestCase | |
def test_replace | |
book_topics = ['html', "java", 'css'] | |
book_topics.substitute('java', 'ruby') | |
expected = ['html', 'ruby', 'css'] | |
assert_equal(expected, book_topics) | |
end | |
end | |
puts [].methods.grep(/^re/).inspect | |
3.times do | |
class C | |
puts "Hello" | |
end | |
end | |
class D | |
def x; 'x'; end | |
end | |
class D | |
def y; 'y'; end | |
end | |
p D.new.x | |
p D.new.y | |
# gems/money-2.1.3/lib/money/core_extensions.rb | |
class Numeric | |
def to_money | |
Money.new(self * 100) | |
end | |
end | |
Money = Struct.new(:cents) | |
cents = 9999 | |
bargain_price = Money.new(cents) #<struct Money cents=9999> | |
standard_price = 100.to_money #<struct Money cents=10000> | |
class MyClass | |
def my_method | |
@v = 1 | |
end | |
end | |
obj = MyClass.new | |
puts obj.class # MyClass | |
puts obj.my_method # 1 | |
puts obj.instance_variables # @v | |
is_inherited = false | |
puts Class.instance_methods(is_inherited).inspect # [superclass, allocate, new] | |
p String.superclass # Object | |
p String.ancestors # [String, Enumerable, Comparable, Object, Kernel] | |
p Enumerable.class # Module | |
p Class.ancestors # [Class, Module, Object, Kernel] | |
module MyModule | |
MyConstant = "Outer constant" | |
class MyClass | |
MyConstant = "Inner Constant" | |
module M2 | |
Module.nesting | |
end | |
end | |
end | |
p MyModule::MyConstant | |
p MyModule::MyClass::MyConstant | |
p ::MyModule::MyConstant # absolute | |
class MyClass; end | |
obj1 = MyClass.new | |
obj2 = MyClass.new | |
obj2.instance_variable_set("@x", 10) | |
puts obj2.instance_variables | |
# object_model/lookup.rb | |
class MyClass | |
def my_method | |
'my method' | |
end | |
end | |
class MySubclass < MyClass | |
end | |
obj = MySubclass.new | |
puts obj.my_method | |
# object_model/lookup_modules.rb | |
module M | |
def m_method | |
puts "instance method from module M" | |
end | |
end | |
class C | |
include M | |
end | |
C.new.m_method | |
# object_model/tangle.rb | |
module Printable | |
def print | |
puts "print from Printable" | |
end | |
def prepare_cover | |
puts "prepare_cover from Printable" | |
end | |
end | |
module Document | |
def print_to_screen | |
prepare_cover | |
format_for_screen | |
end | |
def format_for_screen | |
puts "format_for_screen" | |
end | |
def print | |
puts "print from document" | |
end | |
end | |
class Book | |
include Document | |
include Printable | |
# ... | |
end | |
b = Book.new | |
b.print_to_screen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment