Skip to content

Instantly share code, notes, and snippets.

@markmcspadden
Created October 28, 2011 15:51
Show Gist options
  • Save markmcspadden/1322598 to your computer and use it in GitHub Desktop.
Save markmcspadden/1322598 to your computer and use it in GitHub Desktop.
Ruby Nested Methods
# Scott and I came across some nested classes in some code we maintain.
# We then started talking about nested methods...and then started playing.
# After a couple of "Can you?" examples we asked the important "Why would you ever do this?"
# To be clear, I think nested methods are mostly evil (I think Scott agrees)
# Still, just maybe I can create a use case where using nested methods makes sense...
# The case below is something of a social manners exercise.
# Two instances call casual greeting methods (#hi) and interact in that way.
# However, when an instance calls a formal greeting method (#hello) all instances should elevate to formal methods
# So that's what we have below. I do NOT advocate actually doing this in real code. Enjoy.
class Largeco
class Person
def hello
def hi
puts "hello"
end
def bye
puts "goodbye"
end
hi
end
def hi
puts "hi"
end
def bye
puts "bye-bye"
end
end
end
puts "Scott and Mark are casual"
scott = Largeco::Person.new
mark = Largeco::Person.new
puts "Scott says:"
scott.hi
puts "Mark says:"
mark.hi
puts "Scott says:"
scott.bye
puts "Mark says:"
mark.bye
puts ""
puts "BUT Chris is formal"
puts "Everyone's responses should evalevate to be Formal"
puts ""
chris = Largeco::Person.new
puts "Chris says:"
chris.hello
puts "Scott says:"
scott.hi
puts "Chris says:"
chris.bye
puts "Scott says:"
scott.bye
# Below is the same exercise implemented in a type of modular fashion.
# I like it more than the previous version but still not going to advocate its use
module Formal
def hello
puts "hello"
end
def hi
hello
end
def bye
puts "goodbye"
end
end
module Casual
def bye
puts 'bye-bye'
end
def hi
puts 'hi'
end
def method_missing(method_name, *args, &block)
if Formal.instance_methods.include?(method_name.to_s)
self.class.send :include, Formal
self.send method_name
else
super
end
end
end
class Largeco
class Person
include Casual
end
end
puts "Scott and Mark are casual"
scott = Largeco::Person.new
mark = Largeco::Person.new
puts "Scott says:"
scott.hi
puts "Mark says:"
mark.hi
puts "Scott says:"
scott.bye
puts "Mark says:"
mark.bye
puts ""
puts "BUT Chris is formal"
puts "Everyone's responses should evalevate to be Formal"
puts ""
chris = Largeco::Person.new
puts "Chris says:"
chris.hello
puts "Scott says:"
scott.hi
puts "Chris says:"
chris.bye
puts "Scott says:"
scott.bye
Scott and Mark are casual
Scott says:
hi
Mark says:
hi
Scott says:
bye-bye
Mark says:
bye-bye
BUT Chris is formal
Everyone's responses should evalevate to be Formal
Chris says:
hello
Scott says:
hello
Chris says:
goodbye
Scott says:
goodbye
@pankuznetsov
Copy link

How to access form nested function to variables of outer function? Is it possible in Ruby 2.4.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment