Posts Tagged ‘ruby’

Faulty ruby compilation with rvm: getting ‘undefined symbol: rb_Digest_MD5_Init’ while running racku

Monday, August 23rd, 2010

I am on Ubuntu. Any ideas? Do I have to set any compilation option?

Ruby library for manipulating XML with minimal diffs?

Sunday, August 22nd, 2010

I have an XML file (actually a Visual C# project file) that I want to manipulate using a Ruby script. I want to read the XML into memory, do some work on them that includes changing some attributes and some text (fixing up some path references), and then write the XML file back out. This isn’t so hard.

The hard part is, I want the file I write to look the same as the file I read in, except where I made changes. If the input file used double quotes, I want the output to use double quotes. If the input had a space before />, I want the output to do the same. Basically, I want the output to be the same as the input, except where I explicitly made changes (which, in my case, will only be to attribute values, or to the text content of an element).

I want minimal diffs because this project file is checked into version control — and because the next time I make a change in Visual Studio, it’s going to rewrite it in its preferred format anyway. I want to avoid checking in a bunch of meaningless diffs that will then be changed back again in the near future. I also want to avoid having to open the project in Visual Studio, make a change, and save, before I can commit my Ruby script’s changes. I want my Ruby script to just make its changes, nothing more.

I originally just parsed the file with regexes, but ran into cases where I really needed an XML library because I needed to know more about child elements. So I switched to REXML. But it makes the following undesirable changes to my formatting:

  • It changes all the attributes from double quotes to single quotes.
  • It escapes all the apostrophes inside attribute values (changing them to ').
  • It removes the space before />.
  • It sorts each element’s attributes alphabetically, rather than preserving the original order.

I’m working around this by doing a bunch of gsub calls on REXML’s output, but is there a Ruby XML-manipulation library that’s a better fit for “minimal diff” scenarios?

Compare contents of 2 folders and remove files with the same name that are present in both folders

Sunday, August 22nd, 2010

I am currently writing a quick script in Ruby that goes through all the contents of two folders, and returns a list of all the files that are not in either of the two folders.
Currently what I am doing is storing the paths of all the files of each directory in an array:

Find.find(dir1) do |path|
  if File.file?(path)
    directory1_files << path # Add path to an array of file_paths for the 1st directory.
  end
end # I repeat the process for the second directory and store their paths in an array called directory2_files.

The problem I am having is that when I try and subtract the two arrays (larger array – small array) to get the remaining files, I get an empty array. Reason I get this is because the full paths are trying to be subtracted instead of just the basenames. Ex: ~/folder1/file.txt != ~/folder2/file.txt
How can I find if a file with the same name is in two folders, and remove it from a list so the only files remaining are the ones not present in both folders?

Ruby class-level instance variable

Sunday, August 22nd, 2010

So I was doing a refresher on Ruby and I saw this guy’s blog about creating class-level instance variable in Ruby. I am still trying to understand what the code actually does here. His blog can be found here

http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

and I have created a simple code based on his example to show what I am trying to understand

class Polygon
  class << self; attr_accessor :sides end
  @sides = 10
  def initialize
  end
end

class Triangle < Polygon
  @sides = 3
  class << self; attr_accessor :sides end
  def initialize
  end
end

puts Triangle.sides #3
puts Polygon.sides #10

So the line that I really want to understand is (probably you guys have guessed it),

class << self; attr_accessor :sides end

What does this really do? what is he appending self to class ? is class an array then? Please elaborate as much as you can. Thank you.

What are the popular ‘web-ready’ functional programming languages?

Sunday, August 22nd, 2010

Hello,

This is a simple question: what are the most popular/used/developed (libraries a plus) functional programming languages that are ready to be used for web development?

I don’t mind if they’re pure languages or not, but I would prefer to exclude such languages as Ruby and Python.

Thanks.

Edit 1: Should I make this a wiki?

Edit 2: I’m only aware of Scheme and F#.

Edit 3: Checked out http://stackoverflow.com/questions/1230046/state-of-web-development-using-functional-programming-language which seems to favor a Lisp.

Edit 4: I understand that the languages I’ve mentioned aren’t pure functional programming languages, but I’m just looking for a language that can manipulate types easily.

is there a limit to the number of threads that ruby can run at once?

Sunday, August 22nd, 2010

if not whats the maximum while still remaining efficient?

im creating 14 threads, each of which opens a list of URLs(about 500) creates a new thread for each one, which then downloads it, and adds it to a MySQL db. The MySQL pool size is set to 50.

This is a rake task in RoR

Would this work better using Kernal#fork or some other method?

How to split one level array to many arrays in Ruby 1.9.2

Sunday, August 22nd, 2010

I have an array like this:

[234, 235 , 343, 445]

I want to convert it to look like this

[[234],[235],[343],[445]]

Is there core library function in ruby 1.9.2 could help me to do this fast? and if not is there a fast way?


I did a small tests

def test1
  array = []
  10000000.times do
    array << rand(1000000)
  end
  time = Time.now
  array.permutation(1).to_a
  puts "test1 (permutation) ---> Time = #{Time.now - time}"
end

def test2
  array = []
  10000000.times do
    array << rand(1000000)
  end
  time = Time.now
  array.zip()
  puts "test2 (zip)---> Time = #{Time.now - time}"
end

def test3
  array = []
  10000000.times do
    array << rand(1000000)
  end
  time = Time.now
  array.map { |a| [a] }
  puts "test3 (map) ---> Time = #{Time.now - time}"
end

test1 #test1 (permutation) ---> Time = 2.235128
test2 #test2 (zip)         ---> Time = 1.537088
test3 #test3 (map)         ---> Time = 2.230127

export variable to previous scope in PHP

Sunday, August 22nd, 2010

Now, before you jump at how you shouldn’t mix scopes: I realize this. However, this is a case where either scope mixing must occur or major code duplication must occur?nothing around it. And I’d prefer scope mixing.

That said, I’d like this code:

function a() {
    $a = "a";
    $b = "b";
    $c = "c";
}
function b() {
    a();
    echo $a . $b . $c;
}
b(); // Output: abc
echo $a; // Should raise a notice that $a is undefined

to be able to work as commented. I know it’s not possible in most languages?I was able to do it in Ruby, though; and wonder if you can do it with PHP.

The names of variables aren’t known beforehand in the real situation.

Again, it’s code duplication or this?absolutely no way around it.

Also, it’d be okay if a had to be something like a('b') or something.


In reality, the code is this:

static function renderError($what, $vararray) {
    foreach($vararray as $key => $val) { /* this foreach is the code we want to decouple */
        $key = 'e_'.$key;
        $$key = htmlspecialchars($val);
    }
    ob_clean();
    exit(eval('?>'.file_get_contents(ROOT."/templates/$what.php")));
}

With a call like E_Render::renderError('NotFound', array( 'requested_url' => '/notfound', 'misspelling' => '/reallynotfound' ));

Then, in templates/NotFound.php, you’d have something like:

<html>
<body>
<?php echo $e_requested_url; ?> could not be found. Did you mean <?php echo $e_misspelling: ?>?
</body>
</html>

We’d really rather not have our template authors do anything more than that…although $e['requested_url'] could be done if nothing better was available.

Rails LDAP login using net/ldap

Sunday, August 22nd, 2010

I am trying to get LDAP authentication to work under Rails.
I have chosen net/ldap since it’s a native Ruby LDAP library.

I have tried all possible stuff but still unable to get it work.
Any ideas?

Reversing CRC32

Saturday, August 21st, 2010

I’m looking for a way to reverse a CRC32 checksum. There are solutions around, but they are either badly written, extremely technical and/or in Assembly. Assembly is (currently) beyond my ken, so I’m hoping someone can piece together an implementation in a higher level language. Ruby is ideal, but I can parse PHP, Python, C, Java, etc.

Any takers?