Your SlideShare is downloading. ×
0
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
InactiveSupport
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

InactiveSupport

990

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
990
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. >> 'foo'.tapp "foo" => "foo"
  • 2. class Object def tapp require 'pp' tap { pp self } end end
  • 3. (1..3).map {|i| i ** 2 }.inject(&:+) tmp = (1..3).map {|i| i ** 2 } pp tmp tmp.inject(&:+)
  • 4. (1..3).map {|i| i ** 2 }.tap {|v| pp v }.inject(&:+) (1..3).map {|i| i ** 2 }.tapp.inject(&:+)
  • 5. >> s = {:foo => {:bar => 1}}.to_struct => #<OpenStruct foo=#<OpenStruct bar=1>> >> s.foo.bar => 1
  • 6. require 'ostruct' class Hash def to_struct OpenStruct.new(inject({}) {|h, (k, v)| h.merge(k => v.is_a?(Hash) ? v.to_struct : v) }) end end
  • 7. CONFIG = { :admin => { :name => 'yasuo' }, ... } CONFIG[:admin][:name]
  • 8. CONFIG = OpenStruct.new( :admin => OpenStruct.new( :name => 'yasuo' ), ... ) CONFIG.admin.name
  • 9. CONFIG = { :admin => { :name => 'yasuo' }, ... }.to_struct CONFIG.admin.name
  • 10. ✓ ✓ ✓ ✓
  • 11. >> h = {:a => 1, :b => 2, :c => 3} => {:a=>1, :b=>2, :c=>3} >> h.pairs_at(:a, :c) => {:a=>1, :c=>3}
  • 12. class Hash def pairs_at(*keys) keys.zip(values_at(*keys)).inject(self.class.new) {|h, (k, v)| h.merge(k => v) } end end
  • 13. class UsersController def create u = User.new(params[:user]) ... end end
  • 14. class UsersController def create u = User.new( :name => params[:user][:name], :age => params[:user][:age] ) ... end end
  • 15. class UsersController def create u = User.new( params[:user].pairs_at(:name, :age) ) ... end end
  • 16. class UsersController def create u = User.new( params[:user].slice(:name, :age) ) ... end end
  • 17. >> 'foo'.try(:capitalize) => "Foo" >> 'foo'.try(:hoge) => nil
  • 18. class Object def try(method, *args, &block) respond_to?(method) ? send(method, *args, &block) : nil end end
  • 19. <%=h @user.sector.name %> <%=h @user.sector && @user.sector.name %> <%=h @user.sector.try(:name) %>
  • 20. >> nil.try(:hoge) => nil >> 1.try(:hoge) NoMethodError: undefined method `hoge' for 1:Fixnum
  • 21. class UsersController def create irb ... end end
  • 22. class Object def irb require 'irb' IRB.setup nil IRB::Irb.new(IRB::WorkSpace.new(binding)).tap {|i| IRB.conf[:MAIN_CONTEXT] = i.context }.eval_input end end
  • 23. >> 'users'.what? 'User' "users".classify == "User" => ["classify"] >> 'User'.what? 'users' "User".tableize == "users" => ["tableize"]
  • 24. ✓ ✓ ✓ ✓
  • 25. ✓ ✓

×