I’m attending OSBridge this week so I’m going to pause the daily code readings. I’ll start them back up on Monday the 7th at the latest. If you’re at OSBridge, feel free to find me. I’m using my favorites to plan which sessions I’m thinking of attending.
Tag: code-reading
Daily Code Reading #6 – Facets Array#pad
This week I’m looking at Facets’ Array extensions. Hash, Array, String, and Integer objects are used frequently in Rails so I can take advantage of them faster than more complex classes. The Code 1 2 3 4 5 6 7 8 9 # File lib/core/facets/array/pad.rb, line 14 def pad(len, val=nil) return dup if self.size >= …
Daily Code Reading #5 – Facets Hash#group_by_value
To finish up this week of code reading, I read through Hash#group_by_value. The Code 1 2 3 4 5 6 # File lib/core/facets/hash/group_by_value.rb, line 42 def group_by_value res = {} each{|k, v| (res[v] ||= []) << k} res end# File lib/core/facets/hash/group_by_value.rb, line 42 def group_by_value res = {} each{|k, v| (res[v] ||= []) << k} …
Daily Code Reading #4 – Facets Hash#reverse_merge
Rails uses Hashes a lot for passing options to methods. I typically use Hash#merge to add any default options to them but I’ve always hated the syntax and frequently got it backwards. 1 options = {:value => 'default', :size => 30}.merge(options)options = {:value => 'default', :size => 30}.merge(options) Facets has a Hash#reverse_merge that gets the …
Daily Code Reading #3 – Facets Hash#to_struct
Today I’m looking at facets’ Hash#to_struct. I use structs in my code when I want to store some complex data but don’t need to full Ruby class. The Code 1 2 3 4 # File lib/core/facets/hash/to_struct.rb, line 12 def to_struct(struct_name) Struct.new(struct_name,*keys).new(*values) end# File lib/core/facets/hash/to_struct.rb, line 12 def to_struct(struct_name) Struct.new(struct_name,*keys).new(*values) end Example 1 2 3 4 …