DJ Jazzy Linefeed | 16 May 03:53
Picon

Differing output for function moved from ruby 1.8 to 1.9

Sup, fools?

This is the Levenshtein function I'm gankin' for my file comparison
project (see "40 million comparison..." thread):

# Levenshtein calculator
# Author: Paul Battley (pbattley <at> gmail.com)
# Modified slightly by John Perkins:
# -- removed $KCODE call

def distance(str1, str2)

  unpack_rule = 'C*'
  s = str1.unpack(unpack_rule)
  t = str2.unpack(unpack_rule)
  n = s.length
  m = t.length

  return m if (0 == n) # stop the madness if either string is empty
  return n if (0 == m)

  d = (0..m).to_a
  x = nil

  (0...n).each do |i|
    e = i + 1
    (0...m).each do |j|
      cost = (s[i] == t[j]) ? 0 : 1
      x = [
        d[j + 1] + 1,   # insertion
(Continue reading)

Mark Dodwell | 16 May 00:06
Picon
Favicon

Create an empty file of certain size

Hi,

I need to create an empty (or at least garbage) file of a certain size.
I'm doing it the way below, but it is rather slow -- any ideas on a
quicker way?

  File.open("tmp.txt", 'w') { |f| 10.megabytes.times { f.write "a" } }

Thanks!

~ Mark
--

-- 
Posted via http://www.ruby-forum.com/.

coderrr | 16 May 00:02
Picon

Matz: can we have rescue/else/ensure available in all blocks?

Hi Matz,

I recently posted a Ruby 1.9 wishlist (http://groups.google.com/group/
ruby-talk-google/browse_thread/thread/d771ffa9fe10b811/
c8b5b0b02bd1e2d4) and the #1 item was to have rescue/else/ensure
available in all blocks w/o the need for begin/end.  It turned out to
be the one thing that everyone agreed on.  I think anyone who has done
a real project in Ruby has run into this.

Is there any way you can get this in 1.9?

This (contrived example):

pages.each do |page|
  page.links.each do |link|
    process link
  rescue MalformedLinkError
    @bad_links << link
  end
rescue MalformedPageError
  @bad_pages << page
end

Is so much nicer than this:

pages.each do |page|
  begin
    page.links.each do |link|
      begin
        process link
(Continue reading)

Matthew Moss | 15 May 20:25
Picon

[SUMMARY] The Turing Machine (#162)

While waiting for 48 hours to pass, several examples of Turing Machine
code were sent in, including one example of over 1,500 lines,
generated by code. Most others were simpler, handcrafted rulesets and
tapes, dealing primarily with the limited input set of binary numbers.
This kept code size smaller and easier to understand.

A thought occurred to me that we might make an Extended Turing
Machine, so we would no longer have to eliminate very repetitive state
information. To take a small portion of one example:

    ...
    mark_start v mark_start v L
    mark_start w mark_start w L
    mark_start x mark_start x L
    mark_start y mark_start y L
    mark_start z mark_start z L
    mark_start _ mark_end   S R
    mark_end   a mark_end   a R
    mark_end   b mark_end   b R
    mark_end   c mark_end   c R
    mark_end   d mark_end   d R
    ...

Using a regular expression for the second argument with an implicit
grouping, we could write our Turing Machines more easily.

    ...
    mark_start [a-z] mark_start $1 L
    mark_start _     mark_end    S R
    mark_end   [a-z] mark_end   $1 R
(Continue reading)

DJ Cole | 15 May 20:16

Ruby 1.9 compatibility and performance

I have pretty much committed to Ruby for several projects - and two
things make me nervous:
1) Ruby seems to be changing fundamental things - like Strings - in 1.9.
The changes are incompatible and seem fundamental (e.g. enumeration),
which seems peculiar given the late date in the evolution of Ruby. Why
make these changes now?
(At least, trying to mix a handful of needed new modules out of top of
tree with 1.8 obviously doesn't work - the core classes are diverged in
a fundamental way)
2) Performance - at least, a few modules, like the default REXML parser.
This thing took about 10 minutes to parse a simple 2 MB file just once.
It's unusable. I had to switch to libxml. Is this a Ruby artifact (e.g.
fundamentals like regular expressions just aren't up to snuff) or just a
bad module?

Is there a complete roadmap (I have had trouble finding one) that can
help me decide if it's too early to use Ruby?
--

-- 
Posted via http://www.ruby-forum.com/.

Fernando Perez | 15 May 18:25

Contionnal sum

Hi,

I am wondering if it is possible to create a conditionnal sum.

For instance I would like to do something like this:

@total_amount = @orders.sum { |order| order.amount if order.paid == true
}
--

-- 
Posted via http://www.ruby-forum.com/.

Renato Veneroso | 15 May 18:21
Picon

What is the bes Ruby's book for beginners?

Hi everybody,

This is my first message to this forum.

I'm interested in learning Ruby and would like to know what book you
think I should start with. I've worked as a developer for over 17 years
and as a Java developer for 3 and a half years. However I don't know
anything about Ruby. So I need a book that teaches Ruby from the
scratch.

What book do you advice me reading? I want a book that is good both in
content and easiness of reading and learning.

Sorry if this question has been posted before. I did a quick search on
the forum trying to find a topic like this one but I didn't find it.

Thanks in advance.
--

-- 
Posted via http://www.ruby-forum.com/.

Mario Ruiz | 15 May 18:21

Access Hash in the same order that was created

I need to access to a Hash in the same order that It was created:

mh=Hash.new()
mh["one"]="1"
mh["two"]="2"
mh["three"]="3"
mh["four"]="4"

mh.each {|val|
  puts val[0]
}

In this example I get:
three
two
one
four

and I would like to get:
one
two
three
four

is that possible?

Thanks
--

-- 
Posted via http://www.ruby-forum.com/.

(Continue reading)

Blake Miller | 15 May 17:51
Picon
Favicon

ssh to execute remote ruby script

I'm using 'net/ssh' to ssh into a OS X server and run a ruby script,
like this:

  Net::SSH.start( "xxx.xxx.xxx", :username => 'xxx', :password => 'xxx'
) do | session |
    shell = session.shell.open
    shell.exec "ruby /usr/local/pgsql/share/migrate.rb
#{params['host']}"
  end

The migrate.rb looks like this:

  require 'rubygems'
  ...some ruby code

For some reason, the script fails to execute anything after the require
'rubygems' line.  If I remove that line, the script will run, but
obviously I cannot use any gems, which I need.

If I ssh via terminal to the server, I can run the script fine with:
ruby /usr/local/pgsql/share/migrate.rb 1

I can't figure out why, when ssh'ing via the net/ssh package, the remote
script cannot include rubygems.

Any ideas?
--

-- 
Posted via http://www.ruby-forum.com/.

(Continue reading)

Christoph Angerer | 15 May 17:17
Picon
Favicon

YARV Bytecode Documentation

Hello *,

does anybody know any (if possible up-to-date)
documentation/specification of the Ruby 1.9 (YARV) bytecodes? The only
docs I could find are:

http://www.atdot.net/yarv/insnstbl.html (a simple table of the opcodes)

and

http://www.atdot.net/yarv/yarvarch.en.html (not much more than a table)

However, both sites seem to be outdated and are lacking some useful
explanations (e.g., what is the op_flag parameter for a send, and many
more); also the sourcecode of the VM itself seems to be totally
undocumented in that regard.

Help would be much appreciated! Thanks in advance,
Christoph
--

-- 
Posted via http://www.ruby-forum.com/.

Parv G. | 15 May 15:43
Picon
Favicon

finding current drive

hello,

Is there a way to find out under what drive my current/active file is?

if the .rb file i'm working on is sitting in c:/code/ruby/myfile.rb, i
would like to find a way to retrieve "c".

Thanks!
--

-- 
Posted via http://www.ruby-forum.com/.


Gmane