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 M1 | |
class << self | |
def included(klass) | |
p klass | |
klass.extend ClassMethods | |
end | |
end | |
def method_i | |
p 'InstanceMethodですよー' |
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
require 'active_support/concern' | |
module M1 | |
# ActiveSupport::Concernをextendしとけばdef included(klass)~とかしなくても勝手にClassMethods取り込んでくれる | |
# ActiveSupport::Concernがincludeで呼び出されるappend_featuresを上書きしてるぽい | |
extend ActiveSupport::Concern | |
def method_i | |
p 'InstanceMethodですよー' | |
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
#without memoizing | |
fibonacci = -> | |
if n > 2 then n else fibonacci(n - 2) + fibonacci(n - 1); | |
for i in [0..10] | |
console.log('i = ' + fibonacci(i)) | |
#memoize | |
fibonacci = do -> | |
memo = [0, 1] |
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
# without memoizing | |
def fibonacci n | |
n < 2 ? n : fibonacci(n - 2) + fibonacci(n - 1) | |
end | |
10.times { |i| p fibonacci(i) } | |
# memoize | |
module Fibonacci | |
@@memo = [0, 1] |
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 define_accessor *args | |
args.each do |prop| | |
self.class_eval do | |
define_method prop do | |
return instance_variable_get("@#{prop}") | |
end | |
define_method "#{prop}=" do |val| | |
return instance_variable_set("@#{prop}", val) |
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
# Before | |
def get_file_names path | |
file_names = [] | |
Dir.glob(path).each do |file| | |
file_names << File.basename(file) if File::ftype(file) == "file" | |
end | |
return file_names | |
end | |
# After Refactoring |
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
require 'active_support/core_ext/module/aliasing' | |
class A | |
def foo | |
'foo' | |
end | |
end | |
class B < A | |
def foo_with_bar |
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
$! raise された例外オブジェクト | |
$" require で読み込まれたファイルの配列 | |
$# | |
$$ 現在のプロセス ID | |
$% | |
$& 正規表現にマッチした箇所の文字列 | |
$' 正規表現にマッチした箇所より後ろの文字列 | |
$( | |
$) | |
$* Ruby スクリプトに指定された引数。ARGV と同じ |
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
#include <stdio.h> | |
int main() { | |
int a = 10; | |
int *pa = &a; | |
int **ppa = &pa; | |
printf("%p\n", pa); | |
printf("%d\n", *pa); | |
printf("%p\n", ppa); |
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
def check_sum val | |
sum = val.to_s.each_char.with_index.inject([]) do |memo, (char, i)| | |
next memo << double_digit_value(char.to_i) if i.odd? | |
memo << char.to_i | |
memo | |
end.inject :+ | |
if (sum % 10).zero? | |
p "#{sum} is divisible by 10. Valid!" | |
else |