Archive for the 'ruby' Category

Rails: #28000Access denied

Tuesday, February 27th, 2007

I’m continuing to mess around with Ruby on Rails on WinXP. I just ran into another problem that took me over an hour to figure out. I set up a database in MySQL, configured my database.yml file, created a model and a controller and launched WEBrick. I put the scaffold line in the controller to generate auto-magical interface and I kept getting this error message:

#28000Access denied for user ‘root’@'localhost’ (using password: NO)
RAILS_ROOT: ./script/../config/..

This was driving me crazy, because my yml file specified to use the “webuser” account instead of the root. And yet, rails insisted on fucking around with the root account. I checked it about a hundred times, changed access privileges on my table every possible way, and scoured the web for similar problems. It seems that quite a few people are running into the same damn issue, and almost no one knows hot to fix it.

Finally after reading through the comments here, I found the solution. Simply run the server in production mode:

ruby script/server -p80 -environment=production

This worked for me just fine. I’m not sure why this was happening. Any ruby experts out there can explain this behavior to me? Oh well, for now I guess that as long as I stay in production, I can avoid that stupid error.

Btw, rails is pretty amazing when it works. It only takes one line of code, and you get a simple, yet surprisingly custom complete interface allowing me to populate, edit and remove stuff from your database table:

Rails Scaffold

How awesome is that?

Strange WEBrick Error

Monday, February 26th, 2007

I was messing around with ruby on my Windows box and for some reason WEBrick kept crashing on me with the following output:

ruby script/server
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with –help for options
[2007-02-26 03:14:11] INFO WEBrick 1.3.1
[2007-02-26 03:14:11] INFO ruby 1.8.5 (2006-08-25) [i386-mswin32]
[2007-02-26 03:14:11] WARN TCPServer Error: Bad file descriptor - bind(2)
c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `initialize’: Bad file descriptor - bind(2) (Errno::EBADF)
from c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `new’
from c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `create_listeners’
from c:/ruby/lib/ruby/1.8/webrick/utils.rb:70:in `each’
from c:/ruby/lib/ruby/1.8/webrick/utils.rb:70:in `create_listeners’
from c:/ruby/lib/ruby/1.8/webrick/server.rb:75:in `listen’
from c:/ruby/lib/ruby/1.8/webrick/server.rb:63:in `initialize’
from c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:24:in `initialize’
from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:58:in `new’
… 7 levels…
from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/server.rb:39
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
from script/server:3

A quick google indicates that I’m not the only one having this issue but it does seem very rare. Curiously enough the solution turned out to be using a different port for the server. For example I did:

ruby script/server -p8080

That seemed to work just fine. Strange… I’m wondering if this might have something to do with my AV or Firewall blocking connections on port 3000 for some reason…

Update 02/26/2007 12:48:56 PM

It seems that Rich found an explanation for this behavior. The workaround is really simple:

Take that fucking PocketPC out of the fucking cradle and run WEBrick again.

It appears that the wcescomm.exe hugs the port 3000 when Microsoft ActiveSync is running.

Learning Ruby (Part 1)

Monday, October 9th, 2006

I’m trying to teach myself Ruby so I figured that I will teach you about it as I go along. This will help me to review what I already know. They say the best way to understand a topic is to try to explain it to someone else ) So I will make this into a cyclical recurring thing. I will start with plain old ruby, but I hope to get to rails sometime soon.

First thing that you notice about Ruby is that everything is an object. And when I say everything I mean it. Ruby has no primitives or C style low level arrays. Everything is implemented as an object. This of course does not mean that you can’t do simple operations on numeric values. In fact ruby makes things much less verbose than many other languages. For example consider the following:

temp = -2.abs

The lack of primitives means that you can call methods on anything. The value of temp will be the absolute value of -2 which is 2. Every single part of the language is implemented this way. The following two statements are equivalent:

temp = 2+2
temp = 2.+(2)

All basic mathematical operators are implemented as methods. So when you add two numbers, you are really calling a method with an argument. This works for absolutely all operators - even the array square brackets:

temp = somearray[1]
temp = somearray.[](1)

How awesome is that? This opens up a whole new range of possible ways to implement certain things. For example in Ruby the simplest iterative loop will look like this:

5.times {|i| puts i }

Note the pipe operator in there. This is Ruby’s syntax for specifying a block argument. At each step of iteration the times method returns a value which then gets assigned to i. The puts is a kernel method equivalent to java’s System.out.println.

You see, puts is simply an alias for Kernel.puts. Kernel is simply a top level, core class that contains all of the most useful Ruby utilities. If you look in the documentation, the kernel method puts is in fact an alias for:

$stdout.puts

What is $stdout? It is a global variable. Most Ruby variables have a local scope contained to the current code block or method. You can indicate scope using one of the following prefixes:

  • $ - global variable accessible to everyone everywhere
  • @ - instance variables used within a class
  • @@ - class variables are equivalent to java’s static variables

In addition, all variables starting with an upper case letter are considered static. Before I go any further I should probably note that all of Ruby’s variables are duck typed. What does that mean? It means that the type is not declared but evaluated at runtime (if it quacks like a duck…).

So how do you initialize a class? It’s easy:

class Foo
def initialize(bar)
@bar = bar
end
def foobar
puts @bar
end
end

You can probably figure out why do we need the @ sign on the instance variables. Ruby does not require you to create initialization block of variables in the class body like many other languages. It also does not require you to initialize all your instance variables in the constructor. Ruby instance variables can be initialized in virtually any method within a class. Thus the only way to distinguish instance variables from locals is via that @ thing.

You also probably figured out that initialize is a generic name for the constructor in each ruby class. If you didn’t then I’m telling you about it right now.

That’s it for today… Obviously, I haven’t covered all the basics yet. I still have to mention Ruby’s implementation of hashes, boolean operations, inheritance and etc… But that’s something I will be tackling next time, in Learning Ruby (Part 2).

In the meantime you can always browse through the Ruby Documentation.