Log in

The Ruby Programming Language's Journal
[Most Recent Entries] [Calendar View] [Friends]

Below are the 20 most recent journal entries recorded in The Ruby Programming Language's LiveJournal:

[ << Previous 20 ]
Thursday, October 14th, 2010
6:58 pm
Ruby Newbie: What's wrong with my code?
I've been having a bash at the Google AI contest, writing my AI in Ruby, with mixed results. I'm just learning the language and I think I have a long way to go but I'm enjoying the process. Anyway, I wrote the following method because I wanted to be able to sort one list according to the values in another list. (Basically, when I start the game, I want to measure how far away every planet is from my starting planets and then sort the list of planets according to how far away they are). This is what I came up with, but in some mysterious way I haven't been able to work out, it keeps crashing. Any ideas why?

def doublesorter(firstlist, secondlist)
  ind = 0
  while ind < firstlist.length - 2
    if firstlist[ind] < firstlist[ind + 1]
      ind += 1
      ind = 0
  return firstlist, secondlist

Any advice or help greatly appreciated.
Friday, September 26th, 2008
1:27 pm
Linux VPS Hosting?
Hello Friends

We are looking to possibly move several shared hosting accounts into one VPS hosting account in order to a) reduce costs b) acquire a higher level of dedicated service and c) make it easier to monitor and maintain our sites. We maintain a collection of fairly low-traffic sites but would still like them to be reliable.

We currently using InMotion Hosting, and were happy with their customer service until they started to consistently update their server configs without notifying us in advance, causing downtimes for us and our clients.

What are your experiences with VPS hosts? What recommendations can you give us? Is VPS the best solution for us?

We are using this is a starting point: http://hosting-review.com/top-10-lists/top-10-vps-hosts.shtml
Wednesday, June 25th, 2008
4:03 am
Arbitrary code execution vulnerabilities
Official news post here:

General rundown of exactly _what_ is being affected:

Discussion forums of patches and such:

Now, my question to you guys, is... has anyone patched their copy of ruby? Anyone have any pointers on patching/upgrading ruby on a production site? My partner, the one who set everything up, is off on his honeymoon and can't be reached. The wannabe security professional side of me understands what the vulnerabilities mean and would very much like to patch and upgrade ruby. But from what I've read on the discussion forum, the releases are said to break stuff, which would be Very Bad™ for a live site.

Looks like we're running:
"Ubuntu 7.10" codename gutsy
ruby 1.8.6 (2007-06-07 patchlevel 36) [x86_64-linux]

crossposted to the ruby_lang community, although that looked relatively dead . . .

(This news is now about 5 days old...)
Saturday, December 22nd, 2007
5:31 pm
Developer-friendly Hosting?
Hello Friends

I am interested in exploring web programming via Lisp, Ruby on Rails, and several of the popular Python frameworks.

Here are the features I am interested in:

* Lisp hosting including access to web servers and/or mod_lisp
* Ruby on Rails hosting
* Python hosting
* Configurable source control repository with public and private areas (SVN/Trac or better)
* SSH access
* MySQL or PostgreSQL database
* PHP support (for third-party software packages)

Has anyone here had similar needs? Would you recommend any particular hosting solution?

Tuesday, June 19th, 2007
10:21 am
Quick question
If anybody's curious about why I want to do ISBN stuff for my 'hello world' program, it's in my blog.

my very first ruby code snippetCollapse )

What I want to know Now This Instant is not why it's a crap program design (which I will probably want to know in a few days' time) but why my first idea for 'chop three characters off the front' didn't work...
Monday, June 18th, 2007
6:41 am
Can anyone reassure me Ruby is OK as a first language?
I've been learning Applescript for about a year. and fear it's teaching me bad habits, especially as I'm learning on my own.

I want to (very cautiously) approach Ruby for learning non-Mac-specific stuff (particularly strings/regexes/unixy/xml stuff) and general program design.

Was also attracted by the fact that there seems to be a big, friendly user community -- and although Applescript has a friendly user community it's *very* Mac-specific, and I'm at the stage where I want to take a step back and learn from the ground up.

I'm now getting worried that I can't find anyone to talk to who's using the same Ruby-as-a-First-Language (considering Applescript as 0.5) approach -- because everybody I've seen on the internet so far who does Ruby seems to be much more intelligent than I am and comes at it from the perspective of being a developer rather than a hobbyist.

If there are any other hobbyist types out there, please friend me at my personal blog (which is a new one devoted to the experience of Being About To Learn Ruby, so far).
Wednesday, June 13th, 2007
12:15 pm
Ruby Newbie: Code Critique Request
Hello Friends

I have played with Ruby tutorials and ploughed through a great deal of the Pickaxe book, but have not really written much of anything on my own.

Finally, an opportunity came up for using Ruby for a practical application: a quick and dirty text-processing script. The script is designed to insert onclick event handlers into HTML anchor tags that point to PDF and other files. Our customer purchased a web analytics package and wanted to be able to track clicks to non-HTML files on their server.

The script uses regular expressions, string manipulation, and basic File IO.

If you have some time, could you review the script? I'm interested in code that is compact, legible, and efficient. Please point out if my approach takes advantage of the correct Ruby concepts. I've already found a place where I might improve efficieny: write a file only if changes were made to the file.

I very much appreciate your time!

Source CodeCollapse )
Wednesday, April 11th, 2007
8:02 pm
Module or Class?

I haven't been programming in ruby for very long, less than a year. Something that I noticed today is that I'm unsure whether I'm declaring methods in "Ruby Way".

I needed to find the modes in an Array of data. Very straightforward (but feel free to critique the code itself in addition to my actual question):

  def mode(array)
    stats = Hash.new(0)
    array.each { |item| stats[item] += 1 }
    stats.select { |item, times| times == stats.values.max }.map {|k| k[0] }

"But wait," I thought. "This can be useful for more things than Arrays. Why not make it a method of the Enumerable module?"

module Enumerable
  def mode
    stats = Hash.new(0)
    self.each { |item| stats[item] += 1 }
    stats.select { |item, times| times == stats.values.max }.map {|k| k[0] }

I then went on to write all the statistical methods I needed into the Enumerable class. This made sense to me, since I write ruby code in a very functional style, and I wanted to be able to say list.map { |n| n**2 }.select { |n| n > 10 }.median, or something similar. But then I remembered that the Math library does not work similarly: I call sqrt(value), as opposed to value.sqrt. It seems to me like putting the methods in the class or mixin makes much more sense: they can be overloaded more easily, so if I introduce quaternions, I don't have to redefine Math.sqrt, I just need to clarify Quaternion#sqrt. I also don't have to worry about getting strange inputs to the methods; it'll be the type I want (poor man's strong typing of methods, so to speak).

What is the Ruby Way or insight in doing these sorts of things? The Math library seems to take a namespace approach, but it feels so right (and so easy) to just extend the types I'm working with.

Also, why doesn't the Enumerable mixin have a size method? I couldn't find any derived (is that the correct term for a mixin?) classes that didn't have one (except Range), and it'd be nice to have a guaranteed way of getting the size or length of any Enumerable class.

Saturday, September 23rd, 2006
2:11 am
XP: "An existing connection was forcibly closed" ECONNRESET
(cross-posted from http://www.ruby-forum.com/forum/4)
I am running Ruby 1-185-21 on Win XP without any proxy enabled.
When trying to connect to HTTP server (both server and client are my Ruby scripts) at *localhost* I get this error:
c:/usr/ruby/lib/ruby/1.8/net/protocol.rb:133:in `sysread': An existing connection was forcibly closed by the remote host. (Errno::ECONNRESET)

Interesting facts:
1) Firefox works without any problems with my Ruby server!!!
2) IE doesn't work with my Ruby server!!!
3) My client works fine with all other HTTP servers on the Internet, except for my Ruby server on localhost :(

(see bellow complete trace of this error and source of my simple server and client)Read more...Collapse )
Monday, June 26th, 2006
8:01 pm
Balanced text extraction?
This one should be simple, but I can't seem to find a clear answer via Google, and nothing in the Ruby Application Archive.

Is there something like the Perl Text::Balanced module for Ruby? That is, something that can take this:

(( some text (with a parenthetical comment)))

and extract out this:

some text (with a parenthetical comment)

I know in Perl, for some cases, you can make this work just using a "regular expression" (quoting that because I realize now that a pure RE can't handle this case -- Perl does it via extended operators and this is a known NP sort of problem for the general case). I don't believe Ruby supports these same sort of extensions?

The only mention I see of this at all (thank you Google) is this:


...but that's from 2003, so I'd think someone might have tried to figure out a way to do this by now.

I'm just trying to avoid writing my own parser for this case if I can leverage good code someone else has thought of. Any ideas?
Wednesday, February 15th, 2006
2:09 pm
Newbie trying to connect to a postgres database
Hi all. I'm just getting started with Ruby (coming from PHP), and I'm having trouble connecting to a PostgreSQL database. I'm using the ruby-postgres extension library, but I can't even get PGconn to work. The example given on the rdoc site says to use the 'open' method, which doesn't work. The 'new' method does, but not with a hash of connection paramters, as documented.

I guess what I'd like to know is if there's a better way to work with Postgres from within Ruby, and if anyone can recommend some working example code or a tutorial, since the docs don't seem to be complete or accurate or something.
Wednesday, January 25th, 2006
11:21 pm
preventing variables from being marshaled
I'm using PStore (which uses Marshal) as a quick-n-dirty way to store my app's persistent state. It worked great until I added a Socket instance variable to one of my classes. Sockets can't be marshaled (of course) so now I get an exception.

The docs say to implement _dump and _load methods to override that class's marshaling. But I'm not sure what to make them do. The best I can come up with is to implement _dump by collecting all the other ivars into an array or hash, marshaling that, and returning that string. Then _load does the opposite.

I can make that work. What concerns me is what it will do to cycles in the object graph. If this object's ivars are being marshaled separately, what happens if one of them is an object that's referred to elsewhere in the object graph? Does it get written twice, and (worse) read back in as two separate objects? In my case that would really screw up my app.

What I really want is Java's "transient" attribute...
6:55 pm
event-loop / run-loop library for Ruby?
I'm finally doing some more Ruby programming. Some of it's with Rails, but right now I'm prototyping a type of server. In implementing this, I'm really missing the "RunLoop" facility of Mac OS X. I'm sure this also exists in Windows and Linux — it's an abstract event loop that a thread runs. Most of your code is written as handlers for events, and there are ways to post events to threads.

Is there an implementation of this for Ruby? It's very useful, and it would probably be easy to implement for a Ruby expert [i.e. not me].
Thursday, December 29th, 2005
7:55 pm
rounded corners
OK guys I wrote this yesterday after looking around for some suitable rounded corners code, I noticed there wasn't really a preferred way of doing it in RoR so I picked up the Rico js library and extracted the rounded corners stuff, then wrote these helpers.

I know this is Ruby-Lang but I think a lot of people are also doing RoR so there you have it. Enjoy :)

Basic how to:
1. include rico.js
2. call roundcorners somewhere on the page. ( I do it in the layout )
3. round_div with abandon.

oh i suppose i'll link to the file, if you insist: file is here
Wednesday, November 30th, 2005
10:49 pm
What's a good way to organize library-type code to reuse? I have classes that I'd like to put in a folder called 'library' and be able to access them from scripts in other folders. I can use a relative path in the 'require' statement for the scripts but if the required code 'require's something from the library folder it will not be found. I've been getting around this with this code.

#add the folder to the 'require' search path
require 'kickasscode'
#remove it

Not very elegant but it works. Is there a better way to do this?
Friday, November 11th, 2005
2:46 pm
Monday, October 10th, 2005
1:50 pm
html processing
I'm looking for some recommendations for processing html in ruby. I've tried mechanize, but the docs are non existent and I've been getting 404's from it's site for about a week.
Saturday, September 24th, 2005
12:07 pm

... help. :[

So, I've written some code that parses an XML file and downloads the images linked to in it. The XML bit's irrelevant to this query, as it's working. But, although the directory structure is created fine, and the image is saved into the correct folder, only a portion of it actually appears (I'm downloading gifs; I get the top 8%, say, of the file visible, and the rest just isn't there). I've tried using 'rewind' in front of the file to no effect, and now I'm somewhat stumped. Here's my code:

#										#
# Download images specified in an XML file: Store them via category of product	#
#										#

require "rexml/document"
require "open-uri"

database = File.open( "db.xml" )

doc = REXML::Document.new database

doc.elements.each( "store/product" ) do |product|

	image = product.elements["image"].text
	category = product.elements["category"].text
	name = product.elements["name"].text

	directory = category.downcase.gsub( " ", "_" )
	file = (name + ".gif").downcase.gsub( " ", "_" )
	path = directory + "/" + file

	## make the directory

	current = ""

	directory.each("/") do |dir|

		current << dir

		p current

		Dir.mkdir( current ) unless File.exist?( current )


	## write the file to it

	uri = URI.parse(image)

	uri.open do |f|

		unless File.exist?( path )

			f = File.new( path, "w" )





Monday, September 12th, 2005
2:21 am
DHH has replied to my post - is it normal to feel starstuck?
even so, it's interesting to know

David Heinemeier Hansson <david.heinemeier@gmail.com> Mon, Sep 12, 2005 at 1:30 AM
Reply-To: david.heinemeier@gmail.com
To: @gmail.com, rails@lists.rubyonrails.org
Subject: re:--[form:modelname]-- !--[eoform:modelname]-- what, if anything does this do?
> It looks important and I'm too scared to remove it. The boys on IRC
> are saying it doesn't do anything. Confirm/deny.

It works together with script/update that'll update the HTML/ERb to
reflect the latest from the database. But if you don't want to rely on
that, you can just remove it.

Current Mood: giddy
Sunday, August 21st, 2005
1:56 am
newbie question: dynamically extending class definitions in modules
I'm very confused by the behaviour of the following code (appended). The last two lines print "inner" and "outer" respectively. Why doesn't "class A::C" run in the context of A? Perhaps naively, it appears both quite surprising and that support for dynamically extending classes (defined within modules at least) is broken.

This appears to be expected behaviour. If so, why? Is it possible to dynamically update the definition of A::C so it would act as if it were originally defined within module A?


CONST = "outer"

module A
  CONST = "inner" # This is A::CONST

module A
  class B
    def B.get_const
  class C

class A::C
def (A::C).get_const

puts A::B.get_const #inner
puts A::C.get_const #outer
[ << Previous 20 ]
Ruby Home Page   About LiveJournal.com