Last active
August 29, 2015 14:14
-
-
Save dulao5/7738e937b4a34848e8e0 to your computer and use it in GitHub Desktop.
Ruby初心者の不思議な経験 ref: http://qiita.com/dulao5/items/5bf55262aac6446e719e
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
print("abc") # print "abc" and return nil | |
print "abc" # print "abc" and return nil | |
print() # print "" and return nil | |
print # print "" and return nil | |
# 括弧がなくてもprint()と同じで、print関数の値を取れません | |
method(:print) # => #<Method: Object(Kernel)#print> |
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:2}.map.with_index do |(key,value), index| "#{index} : #{key} => #{value}" end | |
# => ["0 : a => 1", "1 : b => 2"] |
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 MyBase | |
def foo() | |
p 'MyBase::foo' | |
end | |
end | |
class MyTest < MyBase | |
def helpers(&proc) | |
self.class.class_eval(&proc) | |
#selfはMyTestのインスタンスです | |
#self.classはMyTestです | |
end | |
end | |
my_test = MyTest.new | |
my_test.helpers { | |
def foo() | |
p "my_test.foo" | |
super | |
end | |
} | |
my_test.foo | |
# output : | |
# "my_test.foo" | |
# "MyBase::foo" | |
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 'sinatra' | |
get '/hello/:name' do | |
"Hello #{params[:name]}!" | |
# ここのparamsは不思議なものです | |
# なぜブロックの中にSinatra::Requestのparams属性がアクセス出来ます | |
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
# app を実装. | |
# get/post ブロックの中でRack::Requestの属性にアクセス出来ます | |
get do | |
[200, {}, ["hello world // Get : params : " + params.to_s + "\n"]] | |
end | |
post do | |
[200, {}, ["hello world // POST : body : " + body.read + "\n"]] | |
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
# library を実装 | |
require 'rack' | |
class MyRackApp | |
@@process_map = {} | |
def self.register(method, proc) | |
@@process_map[method] = proc | |
end | |
def call(env) | |
request = Rack::Request.new(env) | |
proc = @@process_map[request.request_method] | |
if proc.nil? | |
[500, {}, ["未実装"]] | |
else | |
request.instance_eval(&proc) | |
end | |
end | |
end | |
def get(&proc) | |
MyRackApp.register('GET', proc) | |
end | |
def post(&proc) | |
MyRackApp.register('POST', proc) | |
end | |
# app を実装 | |
get do | |
[200, {}, ["hello world // Get : params : " + params.to_s + "\n"]] | |
end | |
post do | |
[200, {}, ["hello world // POST : body : " + body.read + "\n"]] | |
end | |
# library を実装 | |
rack_app = MyRackApp.new | |
Rack::Handler::WEBrick.run rack_app, :Port=>3000 |
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
enumerator = {a:1, b:2}.map() | |
# => #<Enumerator: {:a=>1, :b=>2}:map> | |
enumerator.method(:with_index) | |
#=> #<Method: Enumerator#with_index> | |
# with_indexはEnumeratorのメソッドです | |
enumerator.with_index() {|(key,value), index| | |
"#{index} : #{key} => #{value}" | |
} | |
#=> ["0 : a => 1", "1 : b => 2"] |
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
文法: | |
method(arg1, arg2, ...) do [`|' 式 ... `|'] 式 ... end | |
method(arg1, arg2, ...) `{' [`|' 式 ... `|'] 式 ... `}' | |
method(arg1, arg2, ..., `&' proc_object) |
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","B","C"].map{|a| a.downcase} | |
#=> ["a", "b", "c"] | |
["A","B","C"].map(&:downcase) | |
#=> ["a", "b", "c"] | |
# &:downcaseは式の形ですけど、実は評価出来ないものです。 | |
&:downcase | |
# SyntaxError: (irb):21: syntax error, unexpected & | |
# &:downcase | |
# ^ | |
# from /usr/local/bin/irb:11:in `<main>' |
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
proc = Proc.new { |a| a.downcase } | |
["A","B","C"].map &proc | |
#=> ["a", "b", "c"] | |
&proc | |
# SyntaxError: (irb):31: syntax error, unexpected & | |
# &proc | |
# ^ | |
# from /usr/local/bin/irb:11:in `<main>' | |
# |
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
file_contents = '' | |
f = File.open('.vimrc', 'r') | |
file_contents = f.read | |
f.close |
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
file_contents = File.open('.vimrc', 'r') {|f| f.read } |
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 my_fopen(name, mode) | |
f = File.open(name, mode) | |
return f unless block_given? | |
begin | |
yield f | |
ensure | |
p "ensure : f.close" | |
f.close | |
end | |
end | |
# style 1: | |
file_contents = '' | |
f = my_fopen('.vimrc', 'r') | |
file_contents = f.read | |
f.close | |
p file_contents | |
# style 2: | |
file_contents = my_fopen('.vimrc', 'r') {|f| f.read } | |
p file_contents |
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
# Sinatra helpers例 | |
helpers do | |
def find_template(views, name, engine, &block) | |
.... | |
super(folder, name, engine, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment