[#306796] Hash#select returns an array but Hash#reject returns a hash... — "Srijayanth Sridhar" <srijayanth@...>

Hello,

21 messages 2008/07/01
[#306820] Re: Hash#select returns an array but Hash#reject returns a hash... — Dave Bass <davebass@...> 2008/07/01

Srijayanth Sridhar wrote:

[#306822] Re: Hash#select returns an array but Hash#reject returns a hash... — "Srijayanth Sridhar" <srijayanth@...> 2008/07/01

irb(main):001:0> a=Hash.new

[#306825] Re: Hash#select returns an array but Hash#reject returns a hash... — Pe, Botp <botp@...> 2008/07/01

From: Srijayanth Sridhar [mailto:srijayanth@gmail.com]=20

[#306835] Re: Hash#select returns an array but Hash#reject returns a hash... — "Srijayanth Sridhar" <srijayanth@...> 2008/07/01

>

[#306838] Re: Hash#select returns an array but Hash#reject returns a hash... — "David A. Black" <dblack@...> 2008/07/01

Hi --

[#306849] Re: Hash#select returns an array but Hash#reject returns a hash... — "Rick DeNatale" <rick.denatale@...> 2008/07/01

On Tue, Jul 1, 2008 at 8:48 AM, David A. Black <dblack@rubypal.com> wrote:

[#306809] Dynamic Variables — Marc Heiler <shevegen@...>

Is there any way in ruby to create dynamic variables?

13 messages 2008/07/01

[#306908] threadify-0.0.1 — ara howard <ara.t.howard@...>

19 messages 2008/07/01

[#306924] workarounds for ruby 1.8.6 segmentations faults — liquid_rails <cheri.anaclerio@...>

Hi,

12 messages 2008/07/01

[#307014] Ranges and Enumerable problems — "Glen Holcomb" <damnbigman@...>

Okay so when I play with "!".."~" wrong things happen.

24 messages 2008/07/02
[#307018] Re: Ranges and Enumerable problems — "Robert Klemme" <shortcutter@...> 2008/07/02

2008/7/2 Glen Holcomb <damnbigman@gmail.com>:

[#307021] Re: Ranges and Enumerable problems — "Glen Holcomb" <damnbigman@...> 2008/07/02

On Wed, Jul 2, 2008 at 9:12 AM, Robert Klemme <shortcutter@googlemail.com>

[#307030] Re: Ranges and Enumerable problems — "Adam Shelly" <adam.shelly@...> 2008/07/02

On 7/2/08, Glen Holcomb <damnbigman@gmail.com> wrote:

[#307035] Re: Ranges and Enumerable problems — "Robert Dober" <robert.dober@...> 2008/07/02

On Wed, Jul 2, 2008 at 7:13 PM, Adam Shelly <adam.shelly@gmail.com> wrote:

[#307037] Re: Ranges and Enumerable problems — "Adam Shelly" <adam.shelly@...> 2008/07/02

On 7/2/08, Robert Dober <robert.dober@gmail.com> wrote:

[#307042] Re: Ranges and Enumerable problems — Robert Klemme <shortcutter@...> 2008/07/02

On 02.07.2008 19:48, Adam Shelly wrote:

[#307050] Re: Ranges and Enumerable problems — "Rick DeNatale" <rick.denatale@...> 2008/07/02

On Wed, Jul 2, 2008 at 2:27 PM, Robert Klemme <shortcutter@googlemail.com>

[#307053] Re: Ranges and Enumerable problems — "Todd Benson" <caduceass@...> 2008/07/02

On Wed, Jul 2, 2008 at 3:50 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:

[#307070] Install/Enable openssl for ruby 1.8.6? — Jason Bornhoft <jbornhoft@...>

I was trying to install Redmine on rails 2.0.2 (this is not a rails

12 messages 2008/07/03

[#307080] thoughts on a more generic Array#partition function — "Rudi Cilibrasi" <cilibrar@...>

An experiment in a more generic partition function. The current

16 messages 2008/07/03
[#307083] Re: thoughts on a more generic Array#partition function — Pe, Botp <botp@...> 2008/07/03

From: Rudi Cilibrasi [mailto:cilibrar@gmail.com]=20

[#307088] Re: thoughts on a more generic Array#partition function — "Rudi Cilibrasi" <cilibrar@...> 2008/07/03

Hi Botp,

[#307095] Re: thoughts on a more generic Array#partition function — Pe, Botp <botp@...> 2008/07/03

From: Rudi Cilibrasi [mailto:cilibrar@gmail.com]=20

[#307136] Re: thoughts on a more generic Array#partition function — "Rick DeNatale" <rick.denatale@...> 2008/07/03

On Thu, Jul 3, 2008 at 12:53 AM, Pe=F1a, Botp <botp@delmonte-phil.com> wrot=

[#307101] Slide Show (S9) Gem Now Includes S5 Support (Including Built-In Gradient Themes) — "Gerald Bauer" <geraldbauer2007@...>

Hello,

21 messages 2008/07/03

[#307153] pseudo-randomize an array in a consistent order — Max Williams <toastkid.williams@...>

Does anyone know how to pseudo-randomize an array (eg with a seed) so

24 messages 2008/07/03

[#307246] Getting Folder Size — Clement Ow <clement.ow@...>

When I use File.size("C:/ruby"), all it returns is 0.

15 messages 2008/07/04

[#307284] from ruby/RoR to Java (framework unknown) :( — S2 <x@...>

My company today decided to ditch ruby development and to develop new web

35 messages 2008/07/04

[#307302] Does Ruby have any advantage over Python to create semantic applications? — Costan <CMValma@...>

Hi all,

10 messages 2008/07/04

[#307414] implementing a simple and efficient index system — Janus Bor <janus@...>

Hello everyone,

18 messages 2008/07/06
[#307415] Re: implementing a simple and efficient index system — phlip <phlip2005@...> 2008/07/06

Janus Bor wrote:

[#307585] Threads: Different behavior under Linux and Windows — Armin Armbruster <aarmbruster@...>

Hi,

13 messages 2008/07/08

[#307654] How to delete a file in Win XP — MAwiniarski <MAwiniarski@...>

Greetings,

18 messages 2008/07/09

[#307667] Thread-safe priority queue? — "Sean O'Halpin" <sean.ohalpin@...>

Hi,

13 messages 2008/07/09

[#307804] Why Ruby interpreter is writed in c (not in c++)? — "Ranieri Teixeira" <ranieri.tx@...>

Hi,

27 messages 2008/07/11
[#307807] Re: Why Ruby interpreter is writed in c (not in c++)? — phlip <phlip2005@...> 2008/07/11

Ranieri Teixeira wrote:

[#307853] Symbolify (#169) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

78 messages 2008/07/11
[#307863] Re: [QUIZ] Symbolify (#169) — "ara.t.howard" <ara.t.howard@...> 2008/07/11

[#307870] Re: [QUIZ] Symbolify (#169) — "Alex LeDonne" <aledonne.listmail@...> 2008/07/11

On Fri, Jul 11, 2008 at 12:12 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:

[#307874] Re: [QUIZ] Symbolify (#169) — "ara.t.howard" <ara.t.howard@...> 2008/07/11

[#307879] Re: [QUIZ] Symbolify (#169) — Dana Merrick <dmerrick@...> 2008/07/11

ara.t.howard wrote:

[#307882] Re: [QUIZ] Symbolify (#169) — "ara.t.howard" <ara.t.howard@...> 2008/07/11

[#307883] Re: [QUIZ] Symbolify (#169) — James Gray <james@...> 2008/07/11

On Jul 11, 2008, at 12:16 PM, ara.t.howard wrote:

[#307933] can ruby replace bash scripts for linux script — "Greg Hauptmann" <greg.hauptmann.ruby@...>

hi,

14 messages 2008/07/12

[#307962] Can't install gems after a new ubuntu install — Max Williams <toastkid.williams@...>

Sorry if this is the wrong forum...

15 messages 2008/07/12

[#307973] regular expressions help — Vivek <krishna.vivek@...>

Hi,

17 messages 2008/07/12

[#308154] Text Editor — Jacob Grover <jacob.grover@...>

Hello, I've been wondering for a long time if there's an Internet text

14 messages 2008/07/14

[#308240] Is it possible to dynamically extend Test::Unit test cases? — "David Mitchell" <monch1962@...>

Hello list,

10 messages 2008/07/15

[#308264] Array.drop doesn't work — Li Chen <chen_li3@...>

Hi all,

14 messages 2008/07/15
[#308265] Re: Array.drop doesn't work — Frederick Cheung <frederick.cheung@...> 2008/07/15

[#308370] The next number that is not in an array — Tim Conner <crofty_james@...>

I want to increment the current value of a variable to the next number

28 messages 2008/07/16
[#308381] Re: The next number that is not in an array — "David A. Black" <dblack@...> 2008/07/16

Hi --

[#308409] So who's coming to RubyFringe? — Oliver Saunders <oliver.saunders@...>

...I am! Hope you meet some of you guys over here.

11 messages 2008/07/17

[#308509] Records and Arrays (#170) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

13 messages 2008/07/18

[#308525] Multiple GEM repositories — Rob Mauchel <rmauchel@...>

I have a Ruby script which runs fine on my own machine that I'd like to

14 messages 2008/07/18

[#308642] Does "rescue" wihtour argument handle any kind of Exception or not? — Iñaki Baz Castillo <ibc@...>

Hi, I read in this article:

17 messages 2008/07/20
[#308643] Re: Does "rescue" wihtour argument handle any kind of Exception or not? — Stefano Crocco <stefano.crocco@...> 2008/07/20

On Sunday 20 July 2008, I=C3=B1aki Baz Castillo wrote:

[#308645] Re: Does "rescue" wihtour argument handle any kind of Exception or not? — Phlip <phlip2005@...> 2008/07/20

Stefano Crocco wrote:

[#308698] rdoc 2.1.0 Released — Eric Hodel <drbrain@...7.net>

rdoc version 2.1.0 has been released!

25 messages 2008/07/21
[#308723] Re: [ANN] rdoc 2.1.0 Released — Joel VanderWerf <vjoel@...> 2008/07/21

Eric Hodel wrote:

[#308750] Re: [ANN] rdoc 2.1.0 Released — Marcin Raczkowski <mailing.mr@...> 2008/07/21

I started playing with new release and frameless template doesn't work.

[#308759] Re: [ANN] rdoc 2.1.0 Released — Eric Hodel <drbrain@...7.net> 2008/07/22

On Jul 21, 2008, at 14:05 PM, Marcin Raczkowski wrote:

[#308790] Re: [ANN] rdoc 2.1.0 Released — Marcin Raczkowski <mailing.mr@...> 2008/07/22

Eric Hodel wrote:

[#308853] Re: [ANN] rdoc 2.1.0 Released — Eric Hodel <drbrain@...7.net> 2008/07/22

[#308699] protected members or explicit abstract classes? — aidy <aidy.lewis@...>

Hi,

12 messages 2008/07/21

[#308736] read CSV file using csv library — Li Chen <chen_li3@...>

Hi all,

19 messages 2008/07/21
[#308774] Re: read CSV file using csv library — "kranthi reddy" <kranthicu@...> 2008/07/22

Hey you can use faster csv instead using the standard csv ruby library.

[#308817] Re: read CSV file using csv library — Li Chen <chen_li3@...> 2008/07/22

kranthi reddy wrote:

[#308821] Re: read CSV file using csv library — "kranthi reddy" <kranthicu@...> 2008/07/22

Hi,

[#347910] Re: read CSV file using csv library — Frank Guerino <frank.guerino@...> 2009/10/07

kranthi reddy wrote:

[#347914] Re: read CSV file using csv library — Marvin Gülker <sutniuq@...> 2009/10/07

Frank Guerino wrote:

[#308761] bj and rails 2.1 - can't get bj to run jobs — dusty <dusty.doris@...>

I have been trying to setup bj with rails 2.1.0 and am having some

13 messages 2008/07/22

[#308831] simple module for "count my instances" behaviour — Julien Thewys <jt@...>

I want to make a simple module that makes its including classes

11 messages 2008/07/22

[#308847] how to capitalize a number of characters in a word — Cheyne Li <happy.go.lucky.clr@...>

Hi, there

13 messages 2008/07/22

[#308884] Is there a simple way to find a method definition? — Ruby Freak <twscannell@...>

Hi,

12 messages 2008/07/23

[#308909] circular 'require' — Shadowfirebird <shadowfirebird@...>

Hi,

29 messages 2008/07/23
[#308911] Re: circular 'require' — Stefano Crocco <stefano.crocco@...> 2008/07/23

On Wednesday 23 July 2008, Shadowfirebird wrote:

[#308919] Re: circular 'require' — Calamitas <calamitates@...> 2008/07/23

On Wed, Jul 23, 2008 at 12:18 PM, Stefano Crocco

[#308935] Re: circular 'require' — "Michael T. Richter" <ttmrichter@...> 2008/07/23

On Wed, 2008-07-23 at 20:39 +0900, Calamitas wrote:

[#308940] Re: circular 'require' — Shadowfirebird <shadowfirebird@...> 2008/07/23

I found a very easy way around it. This is what bothers me. If it's

[#308943] Re: circular 'require' — "Michael T. Richter" <ttmrichter@...> 2008/07/23

On Wed, 2008-07-23 at 22:48 +0900, Shadowfirebird wrote:

[#308944] Re: circular 'require' — Shadowfirebird <shadowfirebird@...> 2008/07/23

Yes, I can see now that in your example no order of loading will allow

[#308951] Of GUIs threads and scheduling woes — "Glen Holcomb" <damnbigman@...>

I have a small GUI app that I have written the purposes for it's creation

12 messages 2008/07/23

[#309006] differnce between .nil? , .empty?, .blank? — Sijo Kg <sijo@...>

Hi

11 messages 2008/07/24

[#309074] Simultaneously URL call, is it possible? — Toki Toki <toki84@...>

Hi to all!

22 messages 2008/07/24
[#309080] Re: Simultaneously URL call, is it possible? — matu <m@...> 2008/07/24

Toki Toki wrote:

[#309083] Re: Simultaneously URL call, is it possible? — Toki Toki <toki84@...> 2008/07/24

matu wrote:

[#309089] inline comments in future release? — Mike Schwab <mike.schwab@...>

Are inline comments a potential feature of Ruby 2.0?

25 messages 2008/07/24
[#309101] Re: inline comments in future release? — Tim Hunter <TimHunter@...> 2008/07/24

Mike Schwab wrote:

[#309111] Re: inline comments in future release? — Mike Schwab <mike.schwab@...> 2008/07/25

> If you have so much code on one line that you feel the need for inline

[#309112] Re: inline comments in future release? — "Michael W. Ryder" <_mwryder@...> 2008/07/25

Mike Schwab wrote:

[#309113] Re: inline comments in future release? — Peña, Botp <botp@...> 2008/07/25

RnJvbTogTWljaGFlbCBXLiBSeWRlciBbbWFpbHRvOl9td3J5ZGVyQHdvcmxkbmV0LmF0dC5uZXRd

[#309165] weird backsplash behaviour inside single quotes — "Michal Suchanek" <hramrach@...>

Hello

13 messages 2008/07/25
[#309170] Re: weird backsplash behaviour inside single quotes — "Todd Benson" <caduceass@...> 2008/07/25

On Fri, Jul 25, 2008 at 8:34 AM, Michal Suchanek <hramrach@centrum.cz> wrote:

[#309171] Re: weird backsplash behaviour inside single quotes — "Todd Benson" <caduceass@...> 2008/07/25

On Fri, Jul 25, 2008 at 9:33 AM, Todd Benson <caduceass@gmail.com> wrote:

[#309173] Interesting Array Initialization Typo — Maciej Tomaka <lunatyq@...>

When initializing for example : [ [1, 2], [2, 3], [3, 2] ]

12 messages 2008/07/25

[#309194] hexdump (#171) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

13 messages 2008/07/25

[#309273] Faster Marshaling? — Greg Willits <lists@...>

Exploring options... wondering if there's anything that can replace

13 messages 2008/07/27

[#309278] deaf grandma. — Houston Barnett-gearhart <americanpragmatic@...>

i picked up ruby 2 days ago & have been bustlin' chris pine's "learn to

25 messages 2008/07/27
[#309282] Re: deaf grandma. — "Martin DeMello" <martindemello@...> 2008/07/27

On Sat, Jul 26, 2008 at 8:31 PM, Houston Barnett-gearhart

[#309322] Re: deaf grandma. — houston barnett-gearhart <americanpragmatic@...> 2008/07/27

thank you, martin, for your reply. i took your advice & wrote an

[#309341] Re: deaf grandma. — "Martin DeMello" <martindemello@...> 2008/07/28

On Sun, Jul 27, 2008 at 2:32 PM, houston barnett-gearhart

[#309343] Re: deaf grandma. — houston barnett-gearhart <americanpragmatic@...> 2008/07/28

Martin, is there any way I can get in touch with you outside of the

[#309391] Re: deaf grandma. — "Martin DeMello" <martindemello@...> 2008/07/28

On Sun, Jul 27, 2008 at 11:51 PM, houston barnett-gearhart

[#309407] Re: deaf grandma. — houston barnett-gearhart <americanpragmatic@...> 2008/07/28

I don't know why this is so hard for me to get my head around, but

[#309387] Win32ole equivalent for Mac OS X — Nathan Loyer <4namlet@...>

Is there an equivalent library for the Win32ole library on the PC? I

12 messages 2008/07/28

[#309441] Concurrent Ruby? — Kyle Murphy <kmurph79@...>

Apologies if this is a really stupid question, I am new to programming,

14 messages 2008/07/29

[#309442] and and or in case — Pe, Botp <botp@...>

Hi All, apologies in advanced if this has been discussed already

12 messages 2008/07/29

[#309472] libxml: is it possible not to use doctype declaration? — "ruud grosmann" <r.grosmann@...>

hi all,

17 messages 2008/07/29
[#309477] Re: libxml: is it possible not to use doctype declaration? — Phlip <phlip2005@...> 2008/07/29

ruud grosmann wrote:

[#309478] Re: libxml: is it possible not to use doctype declaration? — "ruud grosmann" <r.grosmann@...> 2008/07/29

hi Phlip,

[#309488] Re: libxml: is it possible not to use doctype declaration? — Phlip <phlip2005@...> 2008/07/29

ruud grosmann wrote:

[#309572] Re: How to do methodsoverloading in — Jeff Moore <jcmoore@...>

Sunny Bogawat wrote:

12 messages 2008/07/30

[#309589] Suggestions for improving a trivial tag parser — "Gregory Brown" <gregory.t.brown@...>

Hi folks,

11 messages 2008/07/30
[#309593] Re: Suggestions for improving a trivial tag parser — "Robert Dober" <robert.dober@...> 2008/07/30

What about

[#309627] gc doesn't collect? — Roger Pack <rogerpack2005@...>

Any ideas why:

13 messages 2008/07/31

[#309646] super with block — Lou Zell <lzell11@...>

Hi all,

17 messages 2008/07/31
[#309647] Re: super with block — "Jes俍 Gabriel y Gal疣" <jgabrielygalan@...> 2008/07/31

On Thu, Jul 31, 2008 at 11:06 AM, Lou Zell <lzell11@gmail.com> wrote:

[#309692] Re: super with block — Lou Zell <lzell11@...> 2008/07/31

> Hi,

[#309650] Help me with this Numerology code please... — Web Reservoir <webreservoir@...>

Hi,

18 messages 2008/07/31

[#309653] Cool Projects — Alasdair Bell <alasdair@...>

So, anyone working on something awesome?

31 messages 2008/07/31

[#309676] How to get special directories? — Niklas Baumstark <niklas.baumstark@...>

hi all,

18 messages 2008/07/31

[#309739] RubyGems - update made a mess - help needed with Windows — Becca Girl <cschall@...>

I just did a system update of RubyGems and it just broke my rake test. I

16 messages 2008/07/31

[SUMMARY] Symbolify (#169)

From: "Matthew Moss" <matthew.moss@...>
Date: 2008-07-17 20:21:58 UTC
List: ruby-talk #308449
Apologies for being a bit late... Here's this week's summary.


The primary point of this week's quiz was to highlight the `?`
pseudo-operator...  Simply, a character preceded by `?` is converted
to its ASCII value. (I hesitate to call it a true operator, since it
is almost certainly not defined nor implemented in such manner.
Rather, I expect, it is part of Ruby's lexer and so forms part of the
definition of a number. But, in some ways, it _looks_ like a unary
operator.) Type `?(` into irb and the interpreter will return the
value 40.

Knowing this, the five quiz-permitted characters give easy access to
five integer values.

    > [?(, ?), ?*, ?-, ??]
    => [40, 41, 42, 45, 63]

But I specifically permitted four characters (aside from the `?`) that
can be used for a number of mathematical operations.
Grouping/precedence, addition, subtraction, multiplication and
exponentiation are all possible using those four characters, and so we
can express values aside from the five shown above.

    > ?) - ?(               # 41 - 40
    => 1

    > (?* - ?() ** ?(       # 2 ** 40
    => 1099511627776

Using these techniques, it is possible to form an expression that
evaluates to any number, though perhaps not compactly. Let's look at a
couple solutions.

Our first solution is from _Alex LeDonne_:

    def symbolify(i)
      "??-??" + "-?(--?)" * i
    end

Remember, it's the string output from `symbolify` that, by request of
the quiz specification, must contain only the five permitted
characters. Alex begins with `??-??`. As shove above, `??` evaluates
to 63, so this expression is `63-63`: that is, zero.

I'm going to examine the next string in a few steps, just to make it
clear what's going on. First, add in some whitespace.

    "- ?( - - ?)"

Second, replace the `?` pseudo-operators with appropriate ASCII values.

    "- 40 - - 41"

Third, realize that one of those `-` characters now represent unary
negation operators.

    "- 40 - -41"

Fourth, simply the mathematical identity that subtracting a negative
is the same as adding the positive.

    "- 40 + 41"

Finally, do the math.

    "+ 1"

So now let's look at Alex's solution again, using these equivalent
strings, to understand the intent.

    def symbolify(i)
      "0" + "+1" * i
    end

So the call `symbolify(5)` results in a string that is effectively
`0+1+1+1+1+1`. Which, when evaluated, equals five. It's not the most
efficient way to count, but it is the simplest. Of course, this
conversion was done just to show how Alex's solution produces the
correct values on evaluation. The _actual_ output of `symbolify(5)`
is:

    => "??-??-?(--?)-?(--?)-?(--?)-?(--?)-?(--?)"

I apologize for a bit long-winded explanation, but I wanted to make it
clear for anyone who might be confused about all this. For the rest of
the solutions I look at here, I'll skip the string and symbol details
and look specifically at the algorithm.

As programmers, I certainly expected someone to provide a simple
powers-of-two solution, and _Bill Kelly_ provides one. (Bill golfed
this to a single line, but it is shown here with additional whitespace
for readability.)

    def symbolify(i)
      x = "(?(-?()"
      i.to_s(2).each_byte { |d|
        x="(#{x}*(?*-?()--(?#{(d-8).chr}-?())"
      }
      x
    end

The first line assigns a default value of zero. The second line
enumerates over the digits of the binary representation of the input
(as provided by `to_s(2)`). For each digit, the preceding value of `x`
is doubled (multiplication by `(?*-?()`), and then the next digit is
added in. This is basic binary counting.

What might be a little confusing here is why eight is subtracted from
`d`. Realize that enumerating the binary string using `each_byte` sets
`d` to the ASCII value: in this case, either 48 or 49, the ASCII
values of "0" and "1". Subtracting eight gives us 40 or 41, equivalent
to `?(` or `?)`.

As a final non-cheating solution, I want to look at one from _Dana Merrick_.

    require 'mathn'

    def symbolify(i)
       i.zero?? "??-??":"-"+("(?(-?))-"*i).chop
    end

    def symbolify_short(i)
       return "??-??" if i.zero?
       factors = i.prime_division
       factors.inject("") do |string,pair|
         string << "(" << symbolify(pair.first) << ")**("
                << symbolify(pair.last) << ")*"
       end.chop
    end

Dana's `symbolify` function is very similar to Alex's solution above:
counting by ones. But in the added function, `symbolify_short`, Dana
makes use of `prime_division` from the `mathn` module, which will
calculate the prime factorization of an integer. For example:

    > 360.prime_division
    => [[2, 3], [3, 2], [5, 1]

    > (2 ** 3) * (3 ** 2) * (5 ** 1)
    => 360

This provides an efficient way to represent the inputs while still
adhering to the permitted characters. `symbolify_short` alone isn't
enough, since it can't provide output for the prime numbers, but it
calls on the simpler `symbolify` to take care of that.

Now, I figured there might be some other representations possible,
except that `eval` was going to be too limiting. So I allowed
cheating. And cheats we got, of a few different varieties.

_Chris Shea_ redefines the multiplication operator for numbers like so:

    def symbolify(i)
      Fixnum.class_eval <<-EOD
        def *(other)
          #{i}
        end
      EOD
      '?**?*'
    end

This isn't, of course, a general purpose solution, and fails the
delayed evaluation test I provided. Still, Chris managed to return a
string (the same string every time) and have that evaluate to the
number passed in.

Then we have one _Ryan Davis_, which converts the number to base-5,
using the five symbols as the digits:

    FROM, TO = '01234', '?*()-'

    def symbolify n
       n.to_s(5).tr(FROM, TO)
    end

Of course, using `eval` on the result of `symbolify` won't work, so
`eval` had to be redefined as well.

    alias :real_eval :eval
    def eval s
       return real_eval(s) unless s =~ /^[#{Regexp.escape TO}]+$/
       s.tr(TO, FROM).to_i(5)
    end

Another method redefinition comes from _Lucas_ (and done in similar
fashion by a few other Rubyists), which was written solely to pass the
automated tests provided.
    def symbolify (n)
      s = "#{n}"
      def s.delete(*args)
        ""
      end
      s
    end

Note that here Lucas adds a method directly to instance `s` rather
than the String class, so it won't interfere with other code. The sole
intent of this `delete` method is to fool the automated tests.

It didn't fool _me_, as the output clearly contains characters other
than those permitted, but that's okay... I'll let it slide. They're
ugly, but I like 'em.

Finally, I present to you my own disturbingly ugly cheat:

    def symbolify(num)
      def eval(str) $num end; $num = num; "()"
    end

Not only did I redefine `eval` (bad) in a poor manner (badder), but I
started using globals (baddest). Now that you've seen it, I recommend
you never look at it again. Just learn from the experience.

Thanks for all the submissions! I applaud your techniques and
occasional deviousness...



-- 
Matthew Moss <matthew.moss@gmail.com>

In This Thread

Prev Next