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 -*- | |
| def method_missing(funcname, *args) | |
| if funcname =~ /^err([0-9]+)$/ | |
| ret = $1.to_i | |
| if $1.length < 13 | |
| ret *= 10 ** (13 - $1.length) | |
| end | |
| ret | |
| else | |
| super |
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 -*- | |
| module Kernel | |
| def match(obj, &blk) | |
| b = binding | |
| def b.if_that_is *something, &doing | |
| unless @matchers | |
| @matchers = {} | |
| @matchers.default = proc { nil } | |
| 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
| function getattr(self, attrname) { | |
| return (function() { | |
| return eval("this." + attrname); | |
| }).apply(self, arguments); | |
| } | |
| function send(self, funcname) { | |
| return (function() { | |
| return eval("this." + funcname + "(arguments);"); | |
| }).apply(self, arguments); | |
| } |
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 -*- | |
| # usage: ruby find.rb ~ --name .* --depth 1 | |
| # need to OptParser | |
| require "optparser" | |
| $filename = nil | |
| $rootdir = File.expand_path '.' | |
| $recursive = true | |
| OptParser.add_option('--name') { |filename| | |
| $filename = filename |
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 Kernel | |
| def object &definition | |
| # binding | |
| attrs = {} | |
| # object | |
| obj = proc { |attr_name| | |
| { :self => obj, | |
| :dict => function(obj) { |this| this.dict }, | |
| :set => function(obj) { |this, attr_name, value| this.dict[attr_name.to_sym] = value }, |
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 Proc | |
| def apply(binding, *args) | |
| binding.instance_variable_set :@args, args | |
| binding.instance_eval do | |
| def method_missing varname, *args, &blk | |
| return if varname == :to_ary | |
| if !args.empty? || blk | |
| $temp_args = args | |
| $temp_blk = blk | |
| res = eval "#{varname} *$temp_args, &$temp_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
| # Python でもこんな感じのものを定義して | |
| # ["<a>" + e + "</a>" for e in r(['root1', ['branch0', 'branch1'], 'root2', ['branches'] ])] | |
| # って書きたい | |
| class Array | |
| def r &blk | |
| [].tap { |res| | |
| each { |e| | |
| if e.is_a? Array | |
| res << e.r(&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 Kernel | |
| def method_missing(klass_name, *args, &definition) | |
| klass = (self.is_a?(Module) ? self : self.class) | |
| begin | |
| const = klass.const_get(klass_name) | |
| if const.instance_of? Class | |
| define_singleton_method(klass_name) do |*args, &definition| | |
| obj = const.new *args | |
| obj.instance_eval(&definition || proc {}) |
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 -*- | |
| module EndlessRuby | |
| def blank_line? line | |
| return true unless line | |
| (line.chomp.gsub /\s+?/, '') == "" | |
| end | |
| def indent line, level, indent=" " | |
| "#{indent * level}#{line}" |
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/local/bin/ruby | |
| # -*- coding: utf-8 -*- | |
| " | |
| 問題: | |
| ミミは5つあります。ウサミミは3つ、ネコミミは2つです | |
| テトラちゃんと、ミルカさんと、ユーリは、このうちいずれかのミミを頭に付けています。余りはふたつです。 | |
| 自分のミミは見えません。 | |
| 他のふたりのミミは見えます。 | |
| 余りのふたつのミミをなんだか知ることはできません。 |