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
| class Object | |
| def in enum | |
| enum.include? self | |
| end | |
| end | |
| if 'a'.in ['a', 'b', 'c'] | |
| puts 'hello' | |
| end | |
| # => hello |
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
| class Class | |
| def definit *params, &initialize | |
| define_method :initialize do |*args, &blk| | |
| until params.empty? | |
| instance_variable_set :"@#{params.shift.to_sym}", args.shift | |
| end | |
| initialize && instance_exec(*args, &initialize) | |
| end | |
| end |
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
| class Object | |
| def in? enum | |
| enum.include? self | |
| end | |
| end | |
| module Enumerable |
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
| $options = { you: nil }.merge $options || {} | |
| if $options[:you] | |
| puts "hello #{$options[:you]}" | |
| else | |
| puts "hello guest" | |
| end |
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
| class Module | |
| def original method | |
| original_methods = Hash.new do |original_methods, method| | |
| original_methods[method.to_sym] = original_method = [] | |
| define_method :"original_#{method.to_sym}" do |*args, &blk| | |
| fail if original_method.empty? | |
| tmp = original_method.pop | |
| res = tmp.call *args, &blk |
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
| class Array | |
| def === other | |
| zip(other.to_ary).all? { |mine, theirs| mine === theirs } | |
| end | |
| end | |
| puts case [:lover, 'mana', 18] | |
| when [Symbol, String, Integer] then 'love!' |
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
| class Hello | |
| def hello_world *args | |
| puts "hello", *args | |
| end | |
| end |
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
| # -*- coding: utf-8 -*- | |
| class Module | |
| private | |
| def decorators | |
| @decorators ||= [] | |
| def decorator *args, &blk |
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
| module Enumerable | |
| def each_of key, &blk | |
| return Enumerator.new self, :each_of, key unless block_given? | |
| each do |e| | |
| blk.call e[key] | |
| end | |
| end | |
| end | |
| res = [[1, 2], [3, 4]].each_of(0).map do |i| |
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
| # -*- coding: utf-8 -*- | |
| class Module | |
| def define(consname, value=nil, &blk) | |
| if value | |
| const_set(consname, value) | |
| else | |
| define_method(consname, &blk) | |
| end | |
| end |