This file contains 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 Memoize | |
def memoize(*names) | |
names.each do |name| | |
target = instance_method(name) | |
wrapped = lambda do |arg| | |
@_cache_ ||= {} | |
@_cache_[name] ||= {} | |
@_cache_[name][arg] || @_cache_[name][arg] = target.bind(self).call(arg) | |
end | |
define_method name, &wrapped |
This file contains 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
def pass_env(x = 9) | |
binding | |
end | |
def closure(y = 3) | |
Proc.new { y } | |
end | |
pass_env closure do |cl| | |
cl.call |
This file contains 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
def closure(x = 3) | |
Proc.new { x } | |
end | |
a = closure | |
a.call #=> 3 | |
a.binding.eval "x" #=> 3 |
This file contains 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
<?php | |
class RbArray | |
{ | |
function __construct(){ | |
$this->array = $this->get_val(func_get_args()); | |
} | |
private function get_val($val){ | |
if(is_array($val) && is_array($val[0])){ | |
return $this->get_val($val[0]); |
This file contains 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
require 'weather_jp' | |
WeatherJp.parse "東京の今日の天気" | |
#=> #<DayWeather:@city_name = 東京都 東京, @day=今日, @forecast=曇り, @max_temp=25, @min_temp=20, @rain=30> | |
WeatherJp.parse("今日のうどん県の天気教えて下され〜〜").to_s | |
#=> "香川県 高松の今日の天気は曇のち晴 最高気温25度 最低気温17度 降水確率は20% です。" |
This file contains 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
## Simple Usage | |
# creat weather object in differrnt ways | |
tokyo = WeatherJp.get :tokyo | |
tokyo = WeatherJp.get "東京都府中市" | |
minato = WeatherJp::Weather.new("東京都港区") | |
# get weather info as String | |
tokyo.today.to_s | |
#=> can be "東京都 東京の天気は曇りのち晴れ、最高気温34度...etc" |
This file contains 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
# Ruby install 覚え書き | |
# gcc make gcc-c++ zlib-devel httpd-devel openssl-devel curl-devel libreadline-dev readline-dev あたりがはいっているか確認 | |
# install rbenv | |
$ cd | |
$ git clone git://github.com/sstephenson/rbenv.git .rbenv | |
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc | |
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc | |
$ source ~/.bashrc |
This file contains 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
# -*- encoding:utf-8 -*- | |
# | |
# Usage | |
# | |
# tokyo = Weather.new :tokyo | |
# okinawa = Weather.new("沖縄") | |
# fuchu = Weather.new("東京都府中市") | |
# | |
# puts fuchu.get_weather(:today).to_s | |
# puts fuchu.get_weather(0).to_s |
This file contains 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
#include <stdio.h> | |
#define N 7 | |
int min(int* nums, int len); | |
int max(int* nums, int len); | |
int first_appear(int* nums, int len, int target); | |
int main(int argc, char* argv[]) | |
{ | |
int i, len, target; |
This file contains 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
(define (enum-nums proc init limit) | |
(if (> init limit) | |
'() | |
(cons init (enum-nums proc (proc init) limit)))) | |
(define (accumulate op init seq) | |
(if (null? seq) | |
init | |
(+ (car seq) (accumulate op init (cdr seq))))) | |
(accumulate + 0 (enum-nums (lambda (n) (+ 1 n)) 1 100)) |