rvm package install readline
rvm package install iconv
rvm install 1.9.2 -C --enable-shared,--with-iconv-dir=$HOME/.rvm/usr,\
--with-readline-dir=$HOME/.rvm/usr,--build=x86_64-apple-darwin10
Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts
Thursday, July 29, 2010
Installing Ruby 1.9.2 via rvm on OS X
It took me a bit of digging to figure this out (it kept failing with readline errors, and then iconv went missing), so I thought I'd share:
Sunday, March 1, 2009
MySQL-Style Output for the Rails Console
While poking around the database in my rails console, I often found myself jumping to the mysql console just so I could get an easier-to-digest view of the data. I finally had enough of this silliness and wrote up a quick function to dump of set of ActiveRecord objects in the mysql report format.
Grab it from GitHub and stick it in your
>> report(records, :id, :amount, :created_at)
+------+-----------+--------------------------------+
| id | amount | created_at |
+------+-----------+--------------------------------+
| 8301 | $12.40 | Sat Feb 28 09:20:47 -0800 2009 |
| 6060 | $39.62 | Sun Feb 15 14:45:38 -0800 2009 |
| 6061 | $167.52 | Sun Feb 15 14:45:38 -0800 2009 |
| 6067 | $12.00 | Sun Feb 15 14:45:40 -0800 2009 |
| 6059 | $1,000.00 | Sun Feb 15 14:45:38 -0800 2009 |
+------+-----------+--------------------------------+
5 rows in set
Grab it from GitHub and stick it in your
.irbrc.
Thursday, August 21, 2008
Checking for STDIN in ruby
A colleague was asking today how he could see if there's data waiting on
That doesn't work in Linux, though. After some digging, I found this post, which lead to:
Which works on both platforms, but is (a) ugly (catching an exception), and (b) requires you to actually try to read from
In playing around with that, though, I noticed that
I'm not sure how reliable that is on other platforms (it won't work on Windows—fcntl is a Unix-y thing). If I'm understanding fcntl.h correctly, a return value of 2/32770 (0x8002) seems to indicate that the io stream in question is open for writing. I'm not sure if that is intended or just a side-effect. Anyone know?
STDIN in ruby (on Linux). On OS X, this is pretty straightforward:
if $stdin.stat.size > 0
puts "got something: #{STDIN.read}"
else
puts "nada"
end
That doesn't work in Linux, though. After some digging, I found this post, which lead to:
require 'fcntl'
flags = STDIN.fcntl(Fcntl::F_GETFL, 0)
flags |= Fcntl::O_NONBLOCK
STDIN.fcntl(Fcntl::F_SETFL, flags)
begin
puts "got something: #{STDIN.read}"
rescue Errno::EAGAIN
puts "nada"
end
Which works on both platforms, but is (a) ugly (catching an exception), and (b) requires you to actually try to read from
STDIN.In playing around with that, though, I noticed that
STDIN.fcntl(Fcntl::F_GETFL, 0) returned 0 if there was something on STDIN, and something non-zero (2 in OSX and 32770 in Linux) if STDIN was empty. So now the code is simple again:
require 'fcntl'
if STDIN.fcntl(Fcntl::F_GETFL, 0) == 0
puts "got something: #{STDIN.read}"
else
puts "nada"
end
I'm not sure how reliable that is on other platforms (it won't work on Windows—fcntl is a Unix-y thing). If I'm understanding fcntl.h correctly, a return value of 2/32770 (0x8002) seems to indicate that the io stream in question is open for writing. I'm not sure if that is intended or just a side-effect. Anyone know?
Wednesday, February 27, 2008
ConditionsConstructor
I hadn't posted this before because it seemed kind of trivial and silly, but I used it again the other day and decided maybe I should share the joy after all.
Far too often I find myself jumping through hoops building ActiveRecord
Far too often I find myself jumping through hoops building ActiveRecord
find conditions arrays dynamically depending on what arguments were passed in to the method. So I cooked up a simple little class to clean that up a bit:
# class to construct conditions for AR queries
# cc = ConditionsConstructor.new('foobar > ?', 7)
# cc.add('blah is not null')
# cc.add('baz in (?)', [1,2,3])
# cc.conditions
# # => ["foobar > ? and blah is not null and baz in (?)", 7, [1, 2, 3]]
class ConditionsConstructor
def initialize(str = nil, *args)
@conditions_strs = str ? [str] : []
@conditions_args = args
end
def add(str, *args)
@conditions_strs << str
@conditions_args += args
end
def conditions
[@conditions_strs.join(' and ')] + @conditions_args
end
end
Sunday, May 13, 2007
Reloading Ruby Classes in Rails
I ran across a bug in Date::Format today, and after spending a few hours hacking away at a fix (the date/format.rb code is *uuuuugly* and *sloooow*...someone should really rewrite that. Better yet, rewrite it in C), I thought I'd submit a patch. So I grabbed the ruby_1_8 branch and lo and behold, my issue had already been fixed!
So the question now was how to monkeypatch the entire Date::Format module? Simply
First, I tried this:
(note: load needs the actual filename; it doesn't have the magic that require does) but that gave me an error:
Turns out Rails::Plugin::Loader defines its own
et voila!
So the question now was how to monkeypatch the entire Date::Format module? Simply
require-ing it as a plugin doesn't work, since Date::Format is already loaded at that point. The trick then is to use load instead of require.First, I tried this:
load 'date/format.rb'
(note: load needs the actual filename; it doesn't have the magic that require does) but that gave me an error:
in `load': wrong number of arguments (1 for 0) (ArgumentError)
Turns out Rails::Plugin::Loader defines its own
load, so:
Kernel::load 'date/format.rb'
et voila!
Wednesday, February 28, 2007
Do Passwords Right With bcrypt-ruby
My colleague Coda Hale just released a sweet bcrypt-ruby gem that does password hashing right. It also provides for future-proofing by enabling you to assign a computational cost for generating a password hash and letting you version your password hashes. You have no more excuses.
Subscribe to:
Posts (Atom)