Home
Testing 1,2,3... [entries|archive|friends|userinfo]
djberg96

[ website | Sapphire ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| Ruby Home RubyForge RAA comp.lang.ruby Ruby Documentation ]

Test::Unit 2.x, now with TAP [Oct. 22nd, 2009|09:34 am]
[Tags|, , ]
[mood | cheerful]

The test-unit gem now supports a TAP runner.
dberger@file-temp>ruby -Ilib test/test_file_temp.rb --runner tap
1..9
ok 1 - test_file_temp_auto_delete(TC_File_Temp)
ok 2 - test_file_temp_expected_errors(TC_File_Temp)
ok 3 - test_file_temp_name(TC_File_Temp)
ok 4 - test_file_temp_name_basic_functionality(TC_File_Temp)
ok 5 - test_file_temp_no_delete(TC_File_Temp)
ok 6 - test_file_temp_no_delete_with_template(TC_File_Temp)
ok 7 - test_file_temp_threaded(TC_File_Temp)
ok 8 - test_file_temp_tmpdir(TC_File_Temp)
ok 9 - test_file_temp_version(TC_File_Temp)
# Finished in 0.160204 seconds.
# 9 tests, 20 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications

There is also Bacon.
linkpost comment

Back to Rails [Oct. 20th, 2009|02:11 pm]
[Tags|, ]
[mood | annoyed]

So, I'm doing RoR work again, and I'm hitting those annoying little issues that bugged me the first time around.

Today, it's the fact that I have to setup separate validations to handle nils/blanks. IMO I should be able to do this:

validates_numericality_of :elevation, :allow_nil => false

Now, this works. It validates that elevation is a number and it doesn't allow nulls. The problem is that, in the case of a null, it still sets the error message to "not a number". That's not what I want. I want "can't be blank". And I *only* want that error message. To get that effect I have to setup two separate validations, and add a conditional to the second one.
validates_presence_of :elevation

validates_numericality_of :elevation,
  :unless => Proc.new{ |site| site.elevation.blank? }

What a hassle.

Update: Anonymous user posted a better solution. See the comments.
link3 comments|post comment

More random thoughts on mixins, selector namespaces [Oct. 14th, 2009|01:05 pm]
[Tags|]
[mood | contemplative]

Ovid's journal got me thinking about the multi-mixin problem again. That got me thinking about selector namespaces again, too.

Consider the current mixin behavior:
module Alpha
  def hello
    puts "Alpha"
  end
end

module Beta
  def hello
    puts "Beta"
  end
end

class Tango
  include Alpha
  include Beta
end

Tango.new.hello # => "Beta"

Possible solutions:

* First method definition wins
* Last method definition wins, raises a warning
* No one wins, it's an error

And my new, crazy idea:

* First method definition wins, later methods are auto-namespaced

# Let's assume this syntax actually works
t = Tango.new
t.hello       # => "Alpha" (wins, first definition)
t.hello:alpha # => "Alpha" (same, but explicit)
t.hello:beta  # => "Beta"  (calls Beta module's method)

Thoughts?

Update: As per the comments below, you could still have "last definition wins" behavior with the above syntax. Also, "alpha" and "beta" should probably be capitalized in the last example above. I dunno, though. I kinda like the lowercase notation in that context.
link4 comments|post comment

For you Smalltalk/Ruby fans [Sep. 1st, 2009|09:13 pm]
[Tags|, ]
[mood | curious]

I just came across this: http://smalltalk.felk.cvut.cz/projects/smallruby

I've no idea what the actually compatibility is. Still, looks cool.
linkpost comment

FFI Blues [Aug. 7th, 2009|11:04 am]
[Tags|, ]
[Current Location |United States, Colorado, Broomfield]
[mood | blah]

Lately I've been experimenting with FFI, the wrapper around libffi that let's you write C code without ever leaving pure Ruby. I figured with JRuby already using it, and Rubinius and IronRuby on their way, it might be time to start porting some of my C extensions over.

Unfortunately, I've slammed into the cold hard fact that FFI just isn't the grand solution we all hoped it would be. The first problem is that libffi, the underlying source for C based implementations, isn't going to build without the gcc toolchain. That pretty much leaves everyone but Linux, FreeBSD and OS X in the dust, including two heavy hitters, MS Windows and Solaris (if you're using the Sun Studio compiler). Then there's the issue of JRuby's lack of support for certain parts of C, such as file descriptors, as my attempt to port file-temp demonstrated.

So now I'm in a dilemma. If I want to write cross-platform code that will work with JRuby and C based implementations, I'm relegated to keeping two (or more) separate source files, one for MRI and one for JRuby. It would more likely be 3, as I'll still need a separate source file for Windows, since the code is radically different from its *nix counterpart most of the time.

On top of that I've heard disturbing reports that there is little interest in supporting FFI on Windows, in which case we may as well declare it dead in the water. Whether you like it or not, Windows is a major player and its here to stay. If it's not going to work on Windows you may as well chuck it now and stick with C extensions.
link3 comments|post comment

More win32-process updates [Jul. 16th, 2009|08:12 am]
[Tags|, ]
[mood |busy]

We've added a few methods in win32-process to the Process module that were previously unimplemented. Specifically:

* Process.uid
* Process.getpriority
* Process.setpriority

For Process.uid I decided to return the RID of the owning user's SID. This is the same value that you'll get if you look at the local user account id on your Windows box. The only difference is that I allow an optional parameter which, if set to true, will instead return the binary SID. That way you can use it directly if you need to.

One use would be to use it in conjunction with the win32-security library.
require 'win32/process'
require 'win32/security'

p Process.uid # 1000
p Win32::Security::SID.open(Process.uid(true)) # Security object

Implementing Process.getpriority and Process.setpriority was fairly trivial for processes. I just used the GetPriorityClass and SetPriorityClass functions. The only difference is that you can only set the priority for processes, not process groups or users. Also, the values are different (and limited) when compared to its UNIX counterpart.
linkpost comment

Speaking of Readline [Mar. 1st, 2009|06:16 pm]
[Tags|]

The author of the Rawline library has implemented a Readline compatibility mode. Nice.
linkpost comment

Hacking on Rake, too [Jan. 2nd, 2009|06:10 am]
[Tags|, ]
[mood |busy]

In a comment in warnings == errors Piers mentioned that not only does Rails do badly with -w, it does badly with -T.

That inspired me to submit a patch that lets you set the $SAFE level in your Rake::TestTask. So, assuming Jim accepts the patch (and he's indicated that he will), you can do:
desc "Run the test suite"
Rake::TestTask.new("test") do |t|
   t.warning    = true
   t.verbose    = true
   t.safe_level = 1
   # ...
end

Update: Unfortunately my proposed patch was rejected.
linkpost comment

Hacking on Rubygems [Jan. 1st, 2009|11:02 pm]
[Tags|, ]
[mood |busy]

I sorta got the vibe that Eric was getting a little burned out with rubygems and could use some help, as the bug list seemed to be slowly piling up. So, I decided to get more involved in Rubygems development, and over the last few days I've been going over tickets, closing as many as I can, rejecting a few, and mulling over the feature request list. It's been very productive so far.

It's the least I can do, considering one of the bugs (index generation) was caused by one of my old gems.

In other news, the rubygems documentation on the website is badly outdated, and I'd like to get in there and update what I can. I'm still waiting to hear from Jim on access.

Update: I have access! Thanks Chad & Jim!
link5 comments|post comment

My biggest issue with the Rails/Merb merge [Dec. 29th, 2008|12:06 am]
[Tags|, , ]
[mood | annoyed]

My copy, my beta copy, of Programming Rails 3rd edition, is already obsolete.

In other news I ran "Rails" + "Merb" through some anagram finders, since I thought a new name (or at least a code name) would be in order for Rails 3. Here were a few of my favorites:

* Rare Limbs
* Ramble Sir
* Real Brims
* Marble Sir
* Barr Slime
linkpost comment

Warnings == Errors [Dec. 18th, 2008|09:16 pm]
[Tags|, ]
[mood |determined]

I see Piers has discovered that Rails doesn't run -w clean, and it's not alone.

I feel your pain Piers.

Here's the template for all of my Rake test tasks. You should take heed:
desc "Run the test suite"
Rake::TestTask.new("test") do |t|
   t.warning = true
   t.verbose = true
   # ...
end

A few incredibly obnoxious gcc warnings aside, if you treat warnings as errors you're going to end up with better code, and you might just spot a few bugs to boot.
link2 comments|post comment

iGems [Dec. 11th, 2008|10:41 pm]
[Tags|, ]
[mood | silly]

If Greg Pollack can sell Ruby screencasts for $9, maybe I'll start selling all of my gems for $0.99 per download. I'll have my own gem server and a custom web interface, which I'll call "iGems".

Pretty cool name, eh? I thought of it myself.

WHADDYA THINK?
link1 comment|post comment

Weird session bug [Nov. 5th, 2008|08:09 am]
[Tags|, ]
[mood | confused]

Yesterday I spent about an hour tracking down this bizarro bug I had seen occasionally in the logs but hadn't figured out:
TypeError (singleton can't be dumped):
    C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/session/active_record_store.rb:82:in `dump'

I was finally able to reproduce this consistently. It seemed if a new user tried to log in for the first time they would smack into this bug. But, if I created their user account in advance then all was well. I eventually came to the conclusion that it was the session handling.

Further inspection revealed the culprit. I had previously implemented a feature request that new users from a specific group be given automatic visitor access (instead of simply being denied access). So, the basic code structure looked something like this:
user = User.find(uid)

if !user and login_group == 'some_special_group'
   user = User.create(visitor)
else
   # Deny access
end

session[:user] = user

Except, it turns out that while a user object returned by User.find is compatible with the session object, one returned by User.create was not compatible with the session store, and I'd get the mysterious "Singleton can't be dumped" error.

Why? Couldn't tell you. My workaround is to run User.find again for new users and assign that user object to the session.
linkpost comment

Avoiding Readline [Oct. 23rd, 2008|11:59 am]
[Tags|, ]
[mood | chipper]
[music |Ferry Corsten - Junk]

One of the other issues that cropped up with building Ruby from source on Windows was the lack of support for the GNU/Readline library. It's basically impossible to compile from source using MS VC++. To top it all off, I don't even really know what the thing does other than some sort of terminal key binding thingy. I'd look at the documentation for the Ruby readline library...but there isn't any.

This sucked because I thought I had to have readline support in order to run the Rails console app, for example (which I find very handy). I considered rewriting readline using pure Ruby and the Win32::API library. More on that later.

But first, an aside. What's wrong with the following code?
begin
   require 'readline'
rescue
   # Skip readline support
end

See it? Remember, a rescue clause without an explicit error class defaults to StandardError. The problem is that this code raises a LoadError, which is not a subclass of StandardError. The result is that the failed require raises an error anyway.

I noticed that I was getting weird warnings about lack of readline support when I was doing gem installations. It seemed harmless, but it annoyed me. At first I thought it was some sort of dependency in rubygems, but it turned out to be a bug in rdoc 2.x caused by the code I showed above. Once fixed (and it's already fixed in SCM, thanks Eric) the warning went away.

Back to the Rails console app (i.e. script/console), which in turn uses irb. It was choking with an error about lack of readline support. I had assumed up to this point that you had to have readline support in order to use the Rails console. The error above, however, inspired me to inspect the irb source code to see just what the heck it really needed readline for.

And sure enough, it had the same bug that rdoc 2.x had - it wasn't checking for LoadError properly. Once I made that patch (submitted here), all worked well.

As for the pure Ruby version of readline, there's one out there called Inline. Well, sort of. It's pure Ruby but has a very different, though better, API. I've requested a "readline compatibility mode" so that it can be used as a stand-in replacement for the C extension. We'll see what comes of that.
linkpost comment

Great Lakes Experience [Oct. 14th, 2008|06:17 pm]
[Tags|, ]
[mood |accomplished]

I had a good time at the recent Great Lakes Ruby Bash. I got to see Patrick Hurley and Jim Weirich again, and met some new people, too. You can find my slides for my Test::Unit 2 talk here. I went a little fast through the talk, but on the whole I think it went ok. I need to take a look at something Jim Weirich mentioned from the Shoulda library, regarding test contexts, and see if we can get it added to Test::Unit 2.

In other news, I played lots of games, including Ra, Duel in the Dark, Ticket to Ride, Power Grid, Fury of Dracula and Kutuzov. Of these, only Kutuzov turned out to be a disappointment.

Anyway, good times.
linkpost comment

Ruby, OpenSSL and Zlib [Oct. 2nd, 2008|04:26 pm]
[Tags|, , , ]
[mood | calm]

Building Ruby on Windows manually with VC++ is pretty straightforward. The downside is that two libraries in particular, OpenSSL and Zlib, have always been a PITA to build yourself. Since Rubygems needs Zlib, and Rails needs OpenSSL, this has always been problematic.

The zlib problem has been partially solved by Zliby, a pure Ruby implementation of zlib. You can use zliby.rb (renamed to zlib.rb) for installing gems. The downside is that the GzipWriter class isn't implemented yet, so you can't yet build gems. Still, that's not an issue if don't care about building gems locally.

Building OpenSSL is trickier, but with 0.9.8i it's certainly gotten easier. They've added a Perl configure script to help Windows users out. So long as you have Perl installed (which you can grab from ActiveState) you can install OpenSSL. This is how I did it:
perl Configure VC-WIN32 --prefix=c:/usr/local/openssl
ms\do_nasm
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install

We're not quite done, though. There's a macro bug in the openssl x509.h file you'll need to fix first. Here's the diff:
--- x509.orig   Thu Oct 02 11:30:10 2008
+++ x509.h      Thu Oct 02 11:47:05 2008
@@ -116,6 +116,7 @@
 /* Under Win32 these are defined in wincrypt.h */  #undef X509_NAME
#undef X509_CERT_PAIR
+#undef X509_EXTENSIONS
 #endif

 #define X509_FILETYPE_PEM      1

With that in place, you can now build the Ruby interface to openssl like so:
ruby extconf.rb --with-openssl-dir=c:/usr/local/openssl
nmake
nmake install

When you try to use the Ruby openssl bindings you might get this error:
"The ordinal 284 could not be located in the dynamic link library SSLEAY32.DLL"

The problem is that Ruby is picking up an ssleay32.dll file from another application (several apps tend to bundle it, e.g. PostgreSQL). You'll want to make sure you're using the one you just built by setting your path properly:
set PATH=C:\usr\local\openssl\bin;%PATH%

And you should be good to go.
linkpost comment

Great Lakes Ruby Conference [Sep. 27th, 2008|08:36 pm]
[Tags|, ]
[mood | bouncy]

I'll be at the Great Lakes Ruby Conference on October 11th in Ann Arbor, MI if you want to say hi. I have a good friend in the area, so I figured I'd hit the conference, and hang out with the friend the rest of the weekend playing wargames and drinking beer.

Oh, and two random side notes about Guy Decoux. First, I actually found a "C 101" bug in one of Guy's C extensions. I guess I thought it was a little amusing given what an incredible C programmer he was. Second, Guy published one Perl module on CPAN in 1997 called Table.
linkpost comment

Traits? I don't need no stinking Traits! [Sep. 19th, 2008|08:43 pm]
[Tags|, ]
[mood |accomplished]

Well, it finally happened. Seven years, eight months, 17 days, 8 hours, 12 minutes and 23 seconds since I first started using Ruby, it happened.

I had a method name collision with a mixed-in module. One of my own modules, too. Here's what happened.

I decided to take a stab at fixing Terminator for Windows. It uses the sucktackular tempfile library that ships with the Ruby standard library. I wanted to use my own file-temp, but I don't have my C development environment setup completely for the new laptop. Since I already had a partially complete pure Ruby version for Windows, I decided to go ahead and finish it up.

After adding a couple more functions and constants to the windows-pr library, I hit this snag:
class FileTemp < File
   include Windows::MSVCRT::IO
end

Whoops! You see, Ruby's File class has its own File#close method. But, the Windows::MSVCRT::IO module also has an close method (the C function), which overrode the core File method. Blam!

OMG WHAT WILL YO DO BETTER CALL DR SCHARLI AND IMPLEMENT TRAITS AS A CORE LANGUAGE FEATURE YOU STUPID RUBY PROGRAMMERS I TOLD YOU THIS WOULD HAPPEN SOMEDAY.

Er, no. See, once upon a time, in a gvim editor far, far away, I wrote a library called use to deal with just this scenario. I originally wrote it as an academic exercise to silence some Perl programmers, but lo and behold, I actually needed it now. (Also, I discovered a bug in it that I promptly fixed and released, but never mind that).

So, with my library in hand, I used fine grained mixins to only include the methods from the module that I actually needed:
require 'use'

class File::Temp
   use Windows::MSVCRT::IO, :include => [:tmpfile, :tmpnam]
   ...
end

Fixed!

Er, not quite. It turns out that Windows does not support the mkstemp() function. Writing a custom one is easy, but there was one problem - I needed the MSVCRT open() function to implement it. I happen to know that the open-uri library has a global open method, so I wanted to avoid a conflict.

OMG WHAT ARE YOU GOING TO DO NOW YOU CAN'T MIX IT IN WITHOUT OVERRIDING THE OPEN-URI METHOD BUT YOU NEED TO MIX IT IN TO IMPLEMENT MKSTEMP YOU SHOULD HAVE USED A REAL LANGUAGE LIKE SCALA YOU STUPID RUBY PROGRAMMER.

Er, relax bud. You see, one of the other things you can do with the use library is alias methods on the fly when you mix them in. So, the final fix was this:
class FileTemp < File
   use Windows::MSVCRT::IO,
      :include => [:tmpfile, :tmpnam],
      :alias   => {:open, :msvcrt_open}
   ...
end

In my custom mkstemp()function then, I just changed 'open' to 'msvcrt_open', and bingo, no worries.

Update: The Japanese wonder team broke something in p237 so this library won't work with that particular release in certain situations. You've been warned.
link6 comments|post comment

New laptop and other stuff [Sep. 12th, 2008|08:04 pm]
[Tags|, , ]

I decided to go out and buy a new laptop this week. It's an HP DV2945SE. Dual core Turion, 14", 4gb ram, low end nvidia graphics card, 320gb hard drive. Came with Vista x64.

Why? Well, the old laptop was going on 3.5 years old, and the secondary fan was starting to rattle, which annoyed me. Second, it only had 1gb of RAM. This was starting to become problematic, especially when working on full sized game maps in Photoshop. Third, despite my efforts to warm up to Windows on VMWare Fusion, it just wasn't working out very well. It was bringing my system (a quad core Xeon with 2gb of RAM) to a crawl, and would sometimes just act weird. Lastly, I decided I wanted a smaller laptop. This one is about 25% smaller and lighter, which I like.

In other news I released win32-eventlog 0.5.0 today, which fixed the event log description issue on Vista and later. Park Heesob is awesome.

I also finished a pure Ruby version of sys-proctable for Solaris 2.8 and later. I figured out that I could, in fact, use IO#sysseek + IO#sysread as Park had originally suggested. That let me remove the io-extra library as a dependency. Yay.
linkpost comment

Wow64 and Vista [Sep. 7th, 2008|08:09 pm]
[Tags|, ]
[mood | annoyed]
[music |Adult Education - Hall & Oates]

A couple of issues in the win32-eventlog library surfaced this week. First, we hit a 64 bit bug that relates to the way WoW64 deals with the system32 directory. You can read the gory details here if you're interested.

That issue was fixed in the 0.4.9 release.

The second issue, and one we haven't fixed yet, is that they've changed the event log API in Vista/2008 and later. The upshot is that the 0.4.9 release will do a better job of picking up event descriptions on Vista, but it still won't necessarily pick up all of them because of the API change.

Man, what a pain.

Update: Park was on the ball, and made a patch pretty fast. Now fixed in 0.5.0.
linkpost comment

navigation
[ viewing | most recent entries ]
[ go | earlier ]

Advertisement