ruby
Ruby is a programming #oop language!
Ruby is a programming #oop language!
I have a lot of code in my project like the following:
class Animal < ActiveRecord::Base
state_machine do
around_transition :on => :sure do |animal, _, block|
animal.transaction do
block.call
animal.do_things_on_sure
end
end
end
end
around_transition looks ugly and repeats for many classes.. Lets refactor it. More under the cut
RVM has been the traditional choice of #ruby management for years.
Such a heavyweight isn't always required though and a lighter alternative is desirable. We can achieve this setup with the tools chruby and ruby-build.
With chruby we can manage multiple Ruby versions; ruby-build allows us to install multiple Ruby versions.
Well, #ruby 2.0 is shiny and all, but where're that refinements thing? More under the cut
ruby-build needs RUBY_CONFIGURE_OPTS=--with-readline-dir=`brew --prefix readline` when installing Ruby 2.0 on OSX. More under the cut
#Ruby #Telnet Timeout::Error class derives from Interrupt exception. Interrupt exception is sent when the user do a ctrl-c (SIGINT). http://www.ruby-doc.org/core-2.0/Interrupt.html.
I think that Timeout::Error should be derive from RuntimeError or from StandardError.
What do you think?
Not so useful but kinda beautiful #ruby
(1..40).reduce(:*)
Sometimes you want to automatically redirect users based on their user-agent string to a different subdomain. You also probably want to give them an option to override the default and visit the alternative site, and it would be even better to remember their decision in a cookie.
Here is how to do it with rack middleware. More under the cut
I am trying to create a calendar table to display the daily availability of every staff member of a company. What I think I need is a header with the days of the month and then a row (with as many cells as the number of the days) for each staff member. The different background color of each cell will represent a different availability status and the staff members change so I want all this to be dynamically generated. Click for More!
A super handy Hash class extension which allows you to manipulate it's keys based on a specific logic. It also adds the option to recursively affect all Hashes found in the structure. More under the cut
When I code in #ruby I often think to myself "I wish there was a way to map a hash". What would something like that look like? more under the cut...
I had two arrays, one of keys that must be in a params hash, one contains keys that can be in the params hash. I did the following to combine those two arrays and use their values to build a new hash with just the necessary params. More under the cut
Have you ever face a situation where unique validations in your #rails app didn't work? Under the cut a little story about this situation in project that I maintain and solution how broken records (duplicates) were fixed. More under the cut
State Machine comes to the rescue when models in your application need statuses and events.
There is a #state_machine gem for #ruby.
I recommend you to spend some time studying basics before you start using it with #rails. More under the cut
Ruby rewritten in Python
http://docs.topazruby.com/en/latest/blog/announcing-topaz/
Discussion: http://news.ycombinator.com/item?id=5177034 (there is some evaluation time comparison)
Is it obvious and expected behavior:?
If we apply where(:some_table => {}) (empty Hash) for joined table, AR append AND (1 = 2) to SQL so empty results
# and even
User.where(:blabla => {})
# => SELECT `users`.* FROM `users` WHERE (1 = 2)
I'm working my way through the ruby_koans and was confused by how array slicing works. More under the cut
Today, I needed to convert #json string to object in #ruby. More under the cut
I'm building a web app that has several models with share functionality (like history, contacts, notes, etc.). Each section of information is presented as a tab using Twitter Bootstrap's "nav-tabs". More under the cut
$ cat access.log | time perl -pe 's/GET/NEWGET/' > /dev/null
2.64 real 2.21 user 0.15 sys
$ cat access.log | time ruby -pe 'gsub "GET", "NEWGET"' > /dev/null
3.76 real 3.21 user 0.19 sys More under the cut