Skip to content

Instantly share code, notes, and snippets.

class A
CONST_A = 1
@@a = 1
@b = 1
def self.get_a
@@a
end
def self.get_b
a = true
b = true
if a && b
puts "hoge"
end
if a and b
puts "moge"
end
a = 1.tap {|i| puts i + i}
a # => 1
b = 1.tap {|i| break i + i}
b # => 2
# >> 2
# -*- encoding: utf-8 -*-
a = 1 + 3
b = 5 + 4
c = a + b # => 13
d = c + b # => 22
require 'active_support/all'
class A
cattr_accessor :hoge
class << self
attr_accessor :moge
end
end
class B < A; end
require 'rmaybe'
# This code returns "C"
"a b c".split[2].upcase # => "C"
# And if you use maybe. ... .end chain, you can get same result.
"a b c".maybe.split[2].upcase.end # => "C"
# This code causes NoMethodError.
"a b c".split[3].upcase
def fib_iter(prev2, prev, remain)
current = prev2 + prev
remain > 0 ? fib_iter(prev, current, remain - 1) : current
end
# n == 0 => 0, n == 1 => 1, n == 2 => 1, n == 3 => 2, n == 4 => 3, n == 5 => 5 ...
def fib(n)
return n if [0, 1].include?(n)
return fib_iter(0, 1, n - 2)
end
# -*- encoding: utf-8 -*-
old_csv = <<CSV
1,aaa
2,bbb
3,ccc
4,ddd
CSV
# -*- encoding: utf-8 -*-
require 'yaml'
YAML::ENGINE.yamler= 'syck'
require 'active_support/all'
require 'json'
json = '{"a":"\\u0000"}'
# -*- encoding: utf-8 -*-
require 'yaml'
YAML::ENGINE.yamler= 'syck'
require 'active_support/all'
require 'json'
json = '{"a":"\\u0000"}'