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 test(str) | |
puts str | |
end | |
test(<<EOS) | |
Hello World. | |
This is a pen. | |
これはテストです | |
EOS |
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
class Filter | |
class << self | |
def run(&block) | |
self.new.instance_eval(&block) | |
end | |
end | |
def method_missing(name, *args) | |
{ method: name, args: args } | |
end |
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
ip = "192.168.10.1" | |
answer = [] | |
ip.split('.').map(&:to_i).each do |num| | |
answer << "%08d" % num.to_s(2) | |
end | |
puts answer.join('.') |
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
ip = "11000000.10101000.00001010.00000001" | |
answer = [] | |
ip.split('.').each do |num| | |
answer << num.to_i(2) | |
end | |
puts answer.join('.') |
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
DATA.each do |ip| | |
answer = [] | |
ip.split('.').map(&:to_i).each do |num| | |
answer << "%08d" % num.to_s(2) | |
end | |
puts answer.join('.') | |
end |
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
a = 1 | |
b = 1 | |
puts a,b | |
10.times do | |
puts a+b | |
a,b = b, a+b | |
end |
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
class Hoge | |
def initialize(word="piyo") | |
@word = word | |
end | |
def to_s | |
@word | |
end | |
end |
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 method_missing(name, *args) | |
if args.length.zero? | |
name.to_s | |
else | |
puts [name.to_s, args.first].join(' ') | |
end | |
end | |
山田 太郎 | |
鈴木 一郎 |
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
5.times do | |
num = [*('1000'..'9999')].sample | |
num_list = num.chars | |
puts "#{num} => #{num_list.join(" + ")} = #{num_list.map(&:to_i).inject(:+)}" | |
end |
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 'benchmark' | |
Benchmark.bm do |x| | |
str = "Ruby" | |
num = 5000000 | |
x.report(:split) do | |
num.times{ str.split('').each(&:upcase) } | |
end |
OlderNewer