Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Created October 23, 2022 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhandley/86fdb340c9eb1b9714f6fb3e6bf43882 to your computer and use it in GitHub Desktop.
Save seanhandley/86fdb340c9eb1b9714f6fb3e6bf43882 to your computer and use it in GitHub Desktop.
irb(main):001:1* def make_methods(&blk)
irb(main):002:1* (1..100).each { |i| define_method("number_#{i}".to_sym) { blk.call(i) } }
irb(main):003:0> end
=> :make_methods
irb(main):004:0>
irb(main):005:0> make_methods { |i| i }
=> 1..100
irb(main):006:0>
irb(main):007:0> puts number_2 # => 2
2
=> nil
irb(main):008:0> puts number_3 # => 3
3
=> nil
irb(main):009:0>
irb(main):010:0> p method(:number_2).source_location
["(irb)", 2]
=> ["(irb)", 2]
irb(main):011:0>
irb(main):012:0> make_methods { |i| i * 2}
=> 1..100
irb(main):013:0>
irb(main):014:0> puts number_2 # => 4
4
=> nil
irb(main):015:0> puts number_3 # => 6
6
=> nil
irb(main):016:0>
irb(main):017:0> p method(:number_2).source_location
["(irb)", 2]
=> ["(irb)", 2]
irb(main):018:0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment