Ruby wish-list

My personal ruby wish-list (for any feedback):

1) the ability to rescue arrays (or some way to rescue multiple classes
without pain), like this:

all_socket_interrupts_array = [SocketError, Errno::EHOSTUNREACH,
Errno::ENETUNREACH]

begin
  # stuff
rescue all_socket_interrupts # non ugly, yet precise!

end

2) a GC that is 'user-definable' (run after this definable threshold,
this often), and (asidedbly), a GC that can run in its own (native)
thread so it doesn't pause execution of normal threads.

3) an ensure block that's uninterruptible, a la:

begin
# do stuff
rescue
# rescue stuff
ensure_uninterruptible # (or call it ensure_critical)
# do stuff which is guaranteed to get run, and not interrupted.
end

4) the optional ability to have it display the whole backtrace on
uncaught exceptions (and also for all existing threads).

Guess that's it :slight_smile:

Any thoughts?
Thanks.
-Roger

···

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

Roger Pack wrote:

My personal ruby wish-list (for any feedback):

1) the ability to rescue arrays (or some way to rescue multiple classes
without pain), like this:

all_socket_interrupts_array = [SocketError, Errno::EHOSTUNREACH,
Errno::ENETUNREACH]

begin
  # stuff
rescue all_socket_interrupts # non ugly, yet precise!

rescue *all_socket_interrupts_array => e

Should work, I think. At least, it appears to under my brief tests...

···

--
Alex

Hi,

1) the ability to rescue arrays (or some way to rescue multiple classes
without pain), like this:

all_socket_interrupts_array = [SocketError, Errno::EHOSTUNREACH,
Errno::ENETUNREACH]

begin
# stuff
rescue all_socket_interrupts # non ugly, yet precise!

end

Now you know you can.

2) a GC that is 'user-definable' (run after this definable threshold,
this often), and (asidedbly), a GC that can run in its own (native)
thread so it doesn't pause execution of normal threads.

I'd rather prefer smarter collector, but it's possible.

GC on its own thread is a different story. Interaction between
collector and mutator may hinder the performance.

3) an ensure block that's uninterruptible, a la:

begin
# do stuff
rescue
# rescue stuff
ensure_uninterruptible # (or call it ensure_critical)
# do stuff which is guaranteed to get run, and not interrupted.
end

It's not as simple as you've expected. First we need to define how
"uninterruptible" section work.

4) the optional ability to have it display the whole backtrace on
uncaught exceptions (and also for all existing threads).

Simple code like:

  begin
    ...
  rescue => e
    puts e.backtrace
  end

would do.

              matz.

···

In message "Re: ruby wish-list" on Mon, 15 Oct 2007 23:23:06 +0900, Roger Pack <rogerpack2005@gmail.com> writes:

2) a GC that is 'user-definable' (run after this definable threshold,
this often), and (asidedbly), a GC that can run in its own (native)
thread so it doesn't pause execution of normal threads.

Is available in JRuby :slight_smile:

4) the optional ability to have it display the whole backtrace on
uncaught exceptions (and also for all existing threads).

Not sure if anyone has mentioned that already, but this is simply a
matter of either changing TRACE_HEAD and TRACE_TAIL #defines, and
recompiling Ruby, or wrapping your "main" in something like

begin
  ...
rescue Object => e
  puts e.message
  print ' '
  puts e.backtrace.join("\n ")
  exit -1
end

···

On Mon, Oct 15, 2007 at 7:23 AM, Roger Pack <rogerpack2005@gmail.com> wrote:

--
Alexey Verkhovsky
CruiseControl.rb [http://cruisecontrolrb.thoughtworks.com]
RubyWorks [http://rubyworks.thoughtworks.com]

Roger Pack wrote:

My personal ruby wish-list (for any feedback):

I guess it's been suggested here before, but I have often wished for a
way to have 'tighter' control over the variables passed to a parameter.
Sometimes duck typing doesn't immediately catch errors, until you run
through certain (possibly rare sequences). So anyway my wish is for
checking of parameters. Fortunately it seems already possible (as about
30% of rush wishes seem :stuck_out_tongue: )

# a class to match

class Number
def Number.matches? param
   return true if param.class == Fixnum or param.class == Float or
param.class == BigDecimal
end
end

def verify_params params_to_verify
  for param, should_match_this_class in params_to_verify do
    if should_match_this_class.respond_to? :matches?
      raise 'poor parameter' unless should_match_this_class.matches?
param
    else
      raise 'poor parameter' unless param.class ==
should_match_this_class
    end
  end

end

def method_1 a, b, c
verify_params a => Number, b => String

end

Ahh. Type checking at last.
Ok you may begin to throw the stones :slight_smile:
I actually like this setup since it helps describe the parameters.
Thanks :slight_smile:

-R

···

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

Works great thanks Alex!

···

> begin
> # stuff
> rescue all_socket_interrupts # non ugly, yet precise!

rescue *all_socket_interrupts_array => e

Should work, I think. At least, it appears to under my brief tests...

--
Alex

Thanks Matz.
Comments on comments:

>2) a GC that is 'user-definable' (run after this definable threshold,
>this often), and (asidedbly), a GC that can run in its own (native)
>thread so it doesn't pause execution of normal threads.

I'd rather prefer smarter collector, but it's possible.

Yeah a smarter collector would be even nicer.

>ensure_uninterruptible # (or call it ensure_critical)

It's not as simple as you've expected. First we need to define how
"uninterruptible" section work.

I agree. One definition would be to mark Thread.critical, then runs the
block, then unmark. I would use it :slight_smile:

>4) the optional ability to have it display the whole backtrace on
>uncaught exceptions (and also for all existing threads).

Simple code like:

  begin
    ...
  rescue => e
    puts e.backtrace
  end

would do.

              matz.

Good point :slight_smile: My suggestions are thinning down quickly :slight_smile:
Thanks.
-Roger

···

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

Alexey Verkhovsky wrote:

  puts e.backtrace.join("\n ")

puts e.backtrace

puts will automatically insert the platform-dependent line-break for
you. (Note that join("\n") is not sufficient to produce proper
line-breaks on Windows).

···

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

Not sure if anyone has mentioned that already, but this is simply a
matter of either changing TRACE_HEAD and TRACE_TAIL #defines, and
recompiling Ruby, or wrapping your "main" in something like

Yeah I've done that before and it's like a breath of fresh air when the
output includes the entire trace. Ahh.

My next wish, ruby-genie!
Some type of variable that holds the 'last return value'
so, for example, in irb

35+89

=> 124

b = _____ # meaning it should be set equal to 124

:slight_smile:

···

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

Alexey Verkhovsky wrote:

4) the optional ability to have it display the whole backtrace on
uncaught exceptions (and also for all existing threads).

Not sure if anyone has mentioned that already, but this is simply a
matter of either changing TRACE_HEAD and TRACE_TAIL #defines, and
recompiling Ruby, or wrapping your "main" in something like

begin
  ...
rescue Object => e
  puts e.message
  print ' '
  puts e.backtrace.join("\n ")
  exit -1
end

Note that $! (which contains the current exception) is still defined
when Ruby is about to exit on an exception, while it's processing the
END blocks. You can get the same effect without wrapping main by adding
your exception printing code in an END block, which can be anywhere:

END { puts $!.backtrace*"\n\t" }

Clifford Heath.

Hi --

···

On Sat, 22 Mar 2008, Roger Pack wrote:

Roger Pack wrote:

# a class to match

class Number
def Number.matches? param
  return true if param.class == Fixnum or param.class == Float or
param.class == BigDecimal
end

def verify_params params_to_verify
for param, should_match_this_class in params_to_verify do
   if should_match_this_class.respond_to? :matches?
     raise 'poor parameter' unless should_match_this_class.matches?
param
   else
     raise 'poor parameter' unless param.class ==
should_match_this_class
   end
end

end

def method_1 a, b, c
verify_params a => Number, b => String

end

Ahh. Type checking at last.

It's class checking actually, not type checking. Type is different
from class in Ruby.

David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
   ADVANCING WITH RAILS, April 14-17 2008, New York City
   CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!

Hi,

At Sat, 22 Mar 2008 07:33:56 +0900,
Roger Pack wrote in [ruby-talk:295325]:

# a class to match

class Number
def Number.matches? param
   return true if param.class == Fixnum or param.class == Float or
param.class == BigDecimal
end
end

Numeric === obj

···

--
Nobu Nakada

Now you know you can.

>2) a GC that is 'user-definable' (run after this definable threshold,
>this often), and (asidedbly), a GC that can run in its own (native)
>thread so it doesn't pause execution of normal threads.

I'd rather prefer smarter collector, but it's possible.

GC on its own thread is a different story. Interaction between
collector and mutator may hinder the performance.

Would the following be a good idea?

When you garbage collect, fork and have the child "check for garbage"
then report back to the parent on which objects are no longer used.

Children and parents can share memory, it appears [1].
So my thought would be to have the parent and child share a "freeable"
list.

When the parent runs out of memory it spawns a child to do garbage
collection, and continues running. The child would propagate a shared
[large'ish] "freeable" list with pointers to memory which is no longer
used. The parent would later notice GC completion through some message,
and then reclaim the memory and free any unused blocks, and reap the
child.

My hypothesis is that this would allow for a single script to execute
faster, especially on dual core machines when only one script is running
[the child could run in the other core]. This would theoretically then
[should it work]: cost more RAM, use about the same total CPU, but allow
the script to continue running [a la macruby] during GC. Should you
have multiple cores available, it should have shorter execution time,
which is, after all, what we're after.

Thoughts? Cautions?

-R

···

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

>ensure_uninterruptible # (or call it ensure_critical)

It's not as simple as you've expected. First we need to define how
"uninterruptible" section work.

I agree. One definition would be to mark Thread.critical, then runs the block, then unmark. I would use it :slight_smile:

Bad idea in light of native threads IMHO. Every construct that halts all threads should be avoided. If you need exclusive access to resources you need to proper synchronize on them.

Good point :slight_smile: My suggestions are thinning down quickly :slight_smile:

:slight_smile:

Cheers

  robert

···

On 15.10.2007 17:24, Roger Pack wrote:

Hi,

At Fri, 15 Feb 2008 06:50:23 +0900,
Suraj Kurapati wrote in [ruby-talk:291073]:

Alexey Verkhovsky wrote:
> puts e.backtrace.join("\n ")

puts e.backtrace

puts will automatically insert the platform-dependent line-break for
you.

Alexey uses indentation.

(Note that join("\n") is not sufficient to produce proper
line-breaks on Windows).

Not true always. It depends on whether the output is opened in
text mode or in binary mode.

···

--
Nobu Nakada

Roger Pack wrote:

My next wish, ruby-genie!
Some type of variable that holds the 'last return value'
so, for example, in irb

35+89

=> 124

b = _____ # meaning it should be set equal to 124

irb(main):001:0> 35+89
=> 124
irb(main):002:0> _
=> 124

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

It's class checking actually, not type checking. Type is different
from class in Ruby.

David

In that case I shall refer to it as "parameter verification" nobody
would disagree with that :slight_smile:

···

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

Next wish :slight_smile:
I wish that you could make arrays more easily. As in

a = [1,2,3]
a.map{|n| n,n }

and it would work :slight_smile:
-R

···

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

Roger Pack wrote:

When you garbage collect, fork and have the child "check for garbage" then report back to the parent on which objects are no longer used.

The check for garbage can't be done concurrently with ruby code:

   a =
   $a = a
   a = nil

depending on how the gc process is scheduled, that empty array may falsely appear to be garbage:

   # start scan
   a =
   # scan globals
   $a = a
   a = nil
   # scan locals

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

I meant: in the light of the fact that native threads will come at a certain point in time. Your suggested feature would make it unnecessarily complex and slow to use native threads (there would have to be regular synchronization on some hidden global lock, which defies the whole purpose of using (native) threads).

  robert

···

On 15.10.2007 18:45, Robert Klemme wrote:

On 15.10.2007 17:24, Roger Pack wrote:

>ensure_uninterruptible # (or call it ensure_critical)

It's not as simple as you've expected. First we need to define how
"uninterruptible" section work.

I agree. One definition would be to mark Thread.critical, then runs the block, then unmark. I would use it :slight_smile:

Bad idea in light of native threads IMHO. Every construct that halts all threads should be avoided. If you need exclusive access to resources you need to proper synchronize on them.