Good notes posted by Mange
RSS feed![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
See Dir#glob
See glob for more usage information and comments.
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
collect/map
If you’d like to use this method for something like Enumerable#collect, you are looking at the wrong place. This method will return the initial integer, not the values from the block.
a = 20.times { |n| n * 2 } #=> 20
Instead, use Range#collect:
a = (0...20).collect { n * 2 }
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Using block version in Ruby < 1.8.7
The block usage was added in 1.8.7, so to get the same functionality in an earlier version of Ruby, you need to utilize the find method.
Here is a quick example:
match = list.find { |l| l.owner == myself } match_index = list.index(match)
If you do some gymnastics, you can have it on one line without extra variables:
match_index = list.index(list.find { |l| l.owner == myself })
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Better autopad numbers
There is a much better way than to use diwadn’s method if you want to pad numbers with zeros. Here’s my recommended way to do it:
"Number: %010d" % 12345 #=> "Number: 0000012345"
It’s very easy. First we begin our placeholder with “%”, then we specify a zero (0) to signify padding with zeros. If we omitted this zero, the number would be padded with spaces instead. When we have done that, just specify the target length of the string. At last a single “d” is placed to signify that we are inserting a number.
Please see String#% and Kernel#sprintf for more information about how to do this.
Here’s another example of how to do it:
12345.to_s.rjust(10, "0") #=> "0000012345"
See String#rjust for more information.
Any of these methods are a lot better than the method outlined below.
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Differences between normal or-assign operator
Differences between this method and normal memoization with ||=:
-
memoize works with false/nil values
-
Potential arguments are memoized
Take the following example:
def allowed? @allowed ||= begin # Big calculation puts "Worked" false end end allowed? # Outputs "Worked" allowed? # Outputs "Worked" again
Since @allowed is set to false (this is also applicable with nil), the ||= operator will move on the the next statement and will not be short-circuited.
When you use memoize you will not have this problem.
def allowed? # Big calculation puts "Worked" false end memoize :allowed? allowed? # Outputs "Worked" allowed? # No output
Now, look at the case where we have parameters:
def random(max=10) @random ||= rand(max) end random # => 4 random # => 4 -- Yay! random(20) # => 4 -- Oops!
Better use memoize again!
def random(max=10) rand(max) end memoize :random random # => 6 random # => 6 -- Yay! random(20) # => 12 -- Double-Yay! random # => 6 -- Head a'splode
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Usage
This defines attr_accessors at a class level instead of instance level.
class Foo cattr_accessor :greeting end Foo.greeting = "Hello"
This could be compared to, but is not the same as doing this:
class Bar class << self attr_accessor :greeting end end Bar.greeting = "Hello"
The difference might not be apparent at first, but cattr_accessor will make the accessor inherited to the instances:
Foo.new.greeting #=> "Hello" Bar.new.greeting # NoMethodError: undefined method `greeting' for #<Bar:0x18e4d78>
This inheritance is also not copy-on-write in case you assumed that:
Foo.greeting #=> "Hello" foo1, foo2 = Foo.new, Foo.new foo1.greeting = "Hi!" Foo.greeting #=> "Hi!" foo2.greeting #=> "Hi!"
This makes it possible to share common state (queues, semaphores, etc.), configuration (max value, etc.) or temporary values through this.
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Empty elements
If you want to output an empty element (self-closed) like “br”, “img” or “input”, use the tag method instead.
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Usage example
Some examples:
# Remove even numbers (1..30).reject { |n| n % 2 == 0 } # => [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29] # Remove years dividable with 4 (this is *not* the full leap years rule) (1950..2000).reject { |y| y % 4 != 0 } # => [1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000] # Remove users with karma below arithmetic mean total = users.inject(0) { |total, user| total += user.karma } mean = total / users.size good_users = users.reject { |u| u.karma < mean }
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Nothing here
You’re probably looking for I18n::Backend::Simple.
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Convert String to Class in Rails
ncancelliere gave us a very useful tip below, and I just want to make an addendum for it:
If you are using Rails, there is a CoreExtension for this called String#constantize.
"Foo::BarKeeper".constantize #=> Foo::BarKeeper
You can use it with String#camelize if you have to convert the name too
"foo/bar_keeper".camelize #=> "Foo::BarKeeper" "foo/bar_keeper".camelize.constantize #=> Foo::BarKeeper
Don’t forget to rescue NameError in case there was an invalid class name. :-)
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Refactoring excessive code for selects
@garg: It is not recommended to have excessive code in the views. You should refactor your code a bit.
<%= f.select(:manufacturer_id, Manufacturer.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %>
could be changed to this:
# in app/helpers/manufacturer_helper.rb def manufacturers_for_select Manufacturer.all.collect { |m| [m.name, m.id] } end # in the view <%= f.select(:manufacturer_id, manufacturers_for_select, :prompt => 'Select') %>
I would look into collection_select though:
<%= f.collection_select(:manufacturer_id, Manufacturer.all, :id, :name, :prompt => 'Select') %>
It’s much more clean and you don’t have to define a helper for it to be readable (altough it’s still quite long).
If you have to do this often, you should define a FormBuilder extension, so you get methods like f.manufacturer_select:
<%= f.manufacturer_select(:manufacturer_id, Manufacturer.all) %>
IMO, most projects should have a custom form builder anyway, so the addition would be very small. This is my personal opinion, so you don’t have to listen to it. :-)
![Default_avatar_30](https://www.gravatar.com/avatar/1b2792536d87aa21bc739c14980fa103?default=http://apidock.com/images/default_avatar_30.png&size=30)
Watch out for syntax errors
Watch out when you are using returning with hashes. If you would write code like
def foo(bars) returning {} do |map| bars.each { |bar| map[bar.first] = bar } end end
you will get a syntax error since it looks like you tried to supply two blocks! Instead you should write it with parenthesis around the hash:
def foo(bars) returning({}) do |map| bars.each { |bar| map[bar.first] = bar } end end