Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created July 29, 2015 03:23
Show Gist options
  • Save patmaddox/d5e8794337347f059f48 to your computer and use it in GitHub Desktop.
Save patmaddox/d5e8794337347f059f48 to your computer and use it in GitHub Desktop.
Messaging - the big idea
class Foo
def foo
puts "I am foo!"
end
def do_foo
# all five are equivalent
foo
self.foo
foo()
self.foo()
send(:foo)
end
def foo=(something)
puts "Now I am #{something}!"
end
def do_foo_assign
foo = "bar" # assigns a local variable - need explicit receiver for assignment methods
self.foo = "bar"
end
end
f = Foo.new
f.do_foo
f.do_foo_assign
p [1,2,3,4].include?(5) # false
p Array.new(4, "hello") # ["hello", "hello", "hello", "hello"]
p (3 + 2) # 5
p (false && true) # false
p (false || true) # true
class Add10
def +(number)
10 + number
end
end
p(Add10.new + 5)
class RandomList
def [](count)
list = []
count.times { list << rand(100) }
list
end
end
p(RandomList.new[3])
class Summing
def initialize(value)
@value = value
end
def &(other)
self.class.new(@value + other.to_i)
end
def inspect
"@value = #{@value}"
end
def to_i
@value
end
end
p(Summing.new(1) & 2 & 3)
# we can really mess with people here!
class Fixnum
def +(other)
self - other
end
end
p(10 + 5)
def slowercase(a_string)
# sleep for 3 seconds
# return a lowercase version of the string
a_string
end
puts slowercase(ARGV[0])
def slowercase(a_string)
# sleep for 3 seconds
sleep 3
# return a lowercase version of the string
a_string.downcase
end
puts slowercase(ARGV[0])
def print_a_line(printer, string)
printer.print_line string
end
class PrintPrinter
def print_line(string)
print(string + "\n")
end
end
class PutsPrinter
def print_line(string)
puts string
end
end
print_a_line PrintPrinter.new, "sweet text"
print_a_line PutsPrinter.new, "sweet text"
class Slowercase
def initialize(slower, caser)
@slower, @caser = slower, caser
end
def slowercase(a_string)
@slower.slow
@caser.lowercase a_string
end
end
class Slower
def slow
sleep 3
end
end
class Lowercaser
def lowercase(a_string)
a_string.downcase
end
end
class Trowercaser
def lowercase(a_string)
end
end
puts Slowercase.new(Slower.new, Lowercaser.new).slowercase("FuNkYcAsE")
# Can you implement Trowercaser#lowercase and make it work? Hint: look at String#tr
# Can you create a new class similar to Slower, which returns immediately, and
# switch on a command-line arg?
class Slowercase
def initialize(slower, caser)
@slower, @caser = slower, caser
end
def slowercase(a_string)
@slower.slow
@caser.lowercase a_string
end
end
class Slower
def slow
sleep 3
end
end
class Faster
def slow
# do nothing (quickly!)
end
end
class Lowercaser
def lowercase(a_string)
a_string.downcase
end
end
class Trowercaser
def lowercase(a_string)
a_string.tr(("A".."Z").to_a.join, ("a".."z").to_a.join)
end
end
puts Slowercase.new(Slower.new, Lowercaser.new).slowercase("FuNkYcAsE")
# Can you implement Trowercaser#lowercase and make it work? Hint: look at String#tr
puts Slowercase.new(Slower.new, Trowercaser.new).slowercase("FuNkYcAsE")
# Can you create a new class similar to Slower, which returns immediately, and
# switch on a command-line arg?
slower = ARGV[0] == "-f" ? Faster.new : Slower.new
puts Slowercase.new(slower, Lowercaser.new).slowercase("FuNkYcAsE")
def do_call(object)
object.call
end
a_proc = lambda { puts "this is a proc" }
do_call a_proc
class MyClass
def some_method
puts "hello from MyClass#some_method"
end
end
object = MyClass.new
a_method_reference = object.method(:some_method)
do_call a_method_reference
class AnotherClass
def call
puts "This is AnotherClass#call"
end
end
do_call AnotherClass.new
class MyClass
def say_hello
puts "hello"
end
def to_proc
lambda { say_hello }
end
end
def do_call(&block)
block.call
end
do_call &MyClass.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment