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 >= …
Tag: facets
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 …
Daily Code Reading #2 – Facets Hash#zipnew
I’m continuing my look at Ruby Facets today, this time looking at another Hash method. I use Hashes everyday in Rails programming so if I can pick up some new techniques, I can put them to use right away. Today’s subject is Hash#zipnew. The Code 1 2 3 4 5 6 # File lib/core/facets/hash/zipnew.rb, line …