Forum: Ruby Calling R from Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
AlexG (Guest)
on 2006-03-29 13:08
(Received via mailing list)
Hi,

I'm interested in making (well using, but I might be prepared to make)
a bridge between Ruby and R. The RSPerl and RSPython packages seem to
do pretty much what I want to do (in Perl and Python).

My first question is does such a thing already exist? I could only find
ruby-rmathlib (a wrapper for some of the math functions included in R)
on the RAA and nothing on rubyforge. For obvious reasons searching for
'R' related topics is always difficult so I may well have missed
something.

I've a basic proof of concept (adapted from the RSPerl source code)
(provisionally called rruby) where by:

require "rruby"
RRuby.new.eval_R("print","me")

Does what you would expect (fires up an embedded R interpeter and
prints "me"), and it seemed simple enough that I thought someone else
might have already done this and had some more advanced code that I
could borrow/look at/contribute to. If not, I will look at converting a
little more of RSPerl tomorrow to see if I can get something actually
useful working.

Alex G.
Wilson B. (Guest)
on 2006-03-29 18:26
(Received via mailing list)
On 3/29/06, AlexG <removed_email_address@domain.invalid> wrote:
> something.
>

How about this?
http://rubyforge.org/projects/r4ruby/
I haven't used it myself, but from the description, it sounds like
what you're looking for.
(Guest)
on 2006-03-29 19:40
(Received via mailing list)
AlexG wrote:
> something.
> could borrow/look at/contribute to. If not, I will look at converting a
> little more of RSPerl tomorrow to see if I can get something actually
> useful working.
>
> Alex G.

Have you thought about simply spawning an inferior process and simply
pass R scripts into the process?  This is basically what emacs does
with their ESS mode and it works wonderfully well.  Depending on the
level of integration that you wanted to achieve between R and Ruby this
could work very well.  I'm not sure how well suited the R system is to
be treated as a library, though I'll confess I haven't really looked
into it.

If one were to simply use an inferior process that you passed straight
R scripts into you could write a set of functions to translate between
Ruby and R speak (array notation, using <- instead of =, parsing
through the response from R to set a global hash of R variables, etc).

I'd be willing to contribute and test stuff as I use R quite a bit for
data analysis.  Its a very, very good language for doing mathematics,
statistics, and modelling that I don't want to redo in Ruby.  Not that
I couldn't I just don't want to repeat the effort.  The biggest problem
is R has a huge lack of string processing features that make setting up
model runs and generating reports that Ruby is quite good at.
Timothy G. (Guest)
on 2006-03-30 03:10
(Received via mailing list)
May I ask what this is for? I've had to learn R recently after working
with Ruby for a while and find myself thinking constantly "This would
be much simpler if I could only use Ruby!". As far as I've worked out
so far:

R's advantages:
Fast matrix library.
Inbuilt graphing.
Don't have to teach statisticians OO concepts.
Specialised.

R's disadvantages:
Procedural, with highly limited function support.
Slow loops.
Depends on blas (and as a result libfortran).
Specialised.

For most purposes, you could do better using Ruby with a good graphing
system (such as gnuplot). There are, of course exceptions, but I would
consider all options first.
unknown (Guest)
on 2006-03-30 03:22
(Received via mailing list)
On Thu, 30 Mar 2006, Timothy G. wrote:

> May I ask what this is for? I've had to learn R recently after working
> with Ruby for a while and find myself thinking constantly "This would
> be much simpler if I could only use Ruby!". As far as I've worked out
> so far:
>
> R's advantages:
> Don't have to teach statisticians OO concepts.
> Specialised.

you really can subtract

> Fast matrix library.
> Inbuilt graphing.

because narray and gnuplot are very good.  narray is __very__ fast.

regards.

-a
AlexG (Guest)
on 2006-03-30 04:56
(Received via mailing list)
Yeah, from the description it is exactly what I was thinking of.
Unfortunately there's no code to download and the last message on
either the rubyforge site or the wiki is from February 2005 - saying
that something would get released in July 2005. Sadly it looks like a
dead project, which kind of implies the whole idea is harder than I
first imagined. Thanks anyway.
AlexG (Guest)
on 2006-03-30 05:26
(Received via mailing list)
Timothy G. wrote:
> May I ask what this is for? I've had to learn R recently after working
> with Ruby for a while and find myself thinking constantly "This would
> be much simpler if I could only use Ruby!".

Like you, I spend most of my time in R wishing I could use Ruby.
However, whatever I'm doing, I always seem find a few R
functions/libraries that are really useful. I could rewrite the
functions in Ruby, but by then I'd have found some more functions that
I needed and so on. A universal .evalR function that I could call from
Ruby would make all that pain go away. The inbulit graphing is also
nice, though as Ara (and yourself) say, gnuplot could replace that
fairly easily.

For me specifically the ability to use some of the code/classes in the
Bioconductor project (http://www.bioconductor.org/) would really be
nice as well - though I don't know how practical that is.

> For most purposes, you could do better using Ruby with a good graphing
> system (such as gnuplot). There are, of course exceptions, but I would
> consider all options first.

Sound advice. I will have a look at trying to get a Ruby-R bridge
working, but perhaps moving everything to Ruby is the simpler option. I
will ponder on this further.

AlexG
M. Edward (Ed) Borasky (Guest)
on 2006-03-30 09:22
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> Have you thought about simply spawning an inferior process and simply
> pass R scripts into the process?  This is basically what emacs does
> with their ESS mode and it works wonderfully well.  Depending on the
> level of integration that you wanted to achieve between R and Ruby this
> could work very well.  I'm not sure how well suited the R system is to
> be treated as a library, though I'll confess I haven't really looked
> into it.
>
The "guts" of how to interface R with C, C++ and Fortran, and how to
build R as a shared library and call it from the "outside world" are
documented in the "Writing R Extensions" manual that comes with the
source (or the Windows installer). I took the course up at Fred
Hutchinson Cancer Research Center in January and we covered this in a
fair amount of detail. The sample code is on line at

http://www.bioconductor.org/workshops/2006/rforbio...

If you want to do any of this magic on Windows, be prepared to download
a lot of tools and be prepared to put aside your creative urges and just
follow directions to the letter. :)

I have never used "RSPerl"; I usually call R scripts from Perl via

    system("R < script.R");

passing parameters as environment variables -- Perl exports them and R
imports them.
> If one were to simply use an inferior process that you passed straight
> R scripts into you could write a set of functions to translate between
> Ruby and R speak (array notation, using <- instead of =, parsing
> through the response from R to set a global hash of R variables, etc).
>
"=" works in R now as an assignment operator. I'm not sure when that
happened; I've gotten into the habit of "<-" and probably don't intend
to switch. ;)
> I'd be willing to contribute and test stuff as I use R quite a bit for
> data analysis.  Its a very, very good language for doing mathematics,
> statistics, and modelling that I don't want to redo in Ruby.  Not that
> I couldn't I just don't want to repeat the effort.  The biggest problem
> is R has a huge lack of string processing features that make setting up
> model runs and generating reports that Ruby is quite good at.
>
R isn't as bad at "scripting" and "regular expression handling" as all
that. It ain't Perl and it ain't Ruby, and if you're at all an
object-oriented programmer, you'll find the way R does objects and
classes totally different from the "real world". Then again, so is the
way Perl does it -- Ruby is a lot more like a "real object-oriented
language."

Of course, when I first started using R, it really sucked at the
character handling, and I wrote a lot of Perl making "CSV" files for R
to handle. Nowadays I just load things into a database and let RODBC do
all the work. :)

I'm sorta kinda looking at a project that would use *Rails* to manage
the database(s), shelling out to "R" for the analysis. I've got very
little lead time, though, so it might have to be just a refactoring of
the hundreds of lines of Perl and R I have now. And I don't have any
Ruby-aware cow-orkers ... that's a risk too.

--
M. Edward (Ed) Borasky

http://linuxcapacityplanning.com
M. Edward (Ed) Borasky (Guest)
on 2006-03-30 09:31
(Received via mailing list)
Timothy G. wrote:
>
> R's disadvantages:
> Procedural, with highly limited function support.
>
Au contraire! R is at heart a functional language with a Lisp ancestry!
What's different about R (and S) is two different ways of doing objects
and classes, neither one of which is remotely like a Java or Ruby
programmer's idea of objects and classes.
> Slow loops.
>
Yeah ... R programmers don't usually use loops -- we use vector and
matrix operations that have the loops written in C. :) R is probably
more like APL than it is like Ruby, although it does have some Lisp-like
and Scheme-like constructs.

> Depends on blas (and as a result libfortran).
>
Depends on blas for linear algebra only. Can hook up to the
machine-tuned Atlas linear algebra library if necessary.

> Specialised.
>
True, R is not a general-purpose environment, although you *can* do
anything in it. If you look at two of its most successful application
areas, bioinformatics and computational finance, you'll find that the
"non-numeric" parts of the job aren't *always* done outside of the R
code. That's the way most of us learned to use it, and old habits die
hard, but I've seen web servers written in R. Check out the "Rpad"
library for some really nifty things you can do.
> For most purposes, you could do better using Ruby with a good graphing
> system (such as gnuplot). There are, of course exceptions, but I would
> consider all options first.
>
Actually, I think that's been done ... one thing Ruby could use is
something along the lines of the Perl Data Language/Library. But an "R
Server" for numerical work would be easy to do.

--
M. Edward (Ed) Borasky

http://linuxcapacityplanning.com
unknown (Guest)
on 2006-03-30 09:46
(Received via mailing list)
In article <removed_email_address@domain.invalid>,
Timothy G. <removed_email_address@domain.invalid> wrote:
>
>R's disadvantages:
>Procedural, with highly limited function support.
>Slow loops.
>Depends on blas (and as a result libfortran).
>Specialised.
>
>For most purposes, you could do better using Ruby with a good graphing
>system (such as gnuplot). There are, of course exceptions, but I would
>consider all options first.
>

This has come up before, but maybe we need a Ruby-based DSL that could
be aimed
squarely at the likes of Matlab and R.  We'd need to distribute it with
a
graphing system so that it would look like graphing was 'built-in'.  We
could
use narray as the basis for fast matrix math.  One could stay at the DSL
level
or one could delve more deeply and learn the underlying Ruby in order to
get
the greatest advantage of this sort of math-oriented DSL: users wouldn't
have
to know Ruby, but if they did it would become a more powerful tool.  irb
could
be used for an interactive mode...

What should we call this?

rcalc
rmath
mathgem
rubysci
?

....  a good name can help something like this get started.

Phil
unknown (Guest)
on 2006-03-30 09:46
(Received via mailing list)
In article <removed_email_address@domain.invalid>,
 <removed_email_address@domain.invalid> wrote:
>
>you really can subtract
>
>> Fast matrix library.
>> Inbuilt graphing.
>
>because narray and gnuplot are very good.  narray is __very__ fast.
>

what are the alternatives to gnuplot?  What does R use for it's graphing
engine?

Phil
unknown (Guest)
on 2006-03-30 09:55
(Received via mailing list)
On Thu, 30 Mar 2006, Phil T. wrote:

> what are the alternatives to gnuplot?

you can make production quality graphics using

   http://theory.kitp.ucsb.edu/~paxton/tioga.html

regards.

-a
unknown (Guest)
on 2006-03-30 09:58
(Received via mailing list)
On Thu, 30 Mar 2006, Phil T. wrote:

>
> rcalc
> rmath
> mathgem
> rubysci
> ?
>
> ....  a good name can help something like this get started.

http://theory.kitp.ucsb.edu/~paxton/tioga_doc/clas...

i realize it's not totally numerical... but he's thinking along the same
lines.  fyi.


-a
M. Edward (Ed) Borasky (Guest)
on 2006-03-30 10:35
(Received via mailing list)
Phil T. wrote:
> This has come up before, but maybe we need a Ruby-based DSL that could be aimed
> squarely at the likes of Matlab and R.
When you say "we", who are you referring to? People who only know Ruby?
As far as *I* am concerned, R, Axiom and Maxima are (open source) Domain
Specific Languages for mathematics. Ruby is a general-purpose
object-oriented language. Sure, Ruby is good for building
domain-specific languages, but you just aren't going to reinvent all
those wheels in Ruby.
> rcalc
> rmath
> mathgem
> rubysci
> ?
>
> ....  a good name can help something like this get started.
>
> Phil
>
There are a few things out there, but nothing with as much horsepower as
R and the two main general purpose symbolic engines, Axiom and Maxima.
There are hundreds of person-decades involved in building those three
packages. In fact, the latter two weren't originally open source. Axiom
was a commercial product, and Maxima was a US government funded project.
R was always an open source project, however.

--
M. Edward (Ed) Borasky

http://linuxcapacityplanning.com
unknown (Guest)
on 2006-03-30 11:05
(Received via mailing list)
In article <removed_email_address@domain.invalid>,
 <removed_email_address@domain.invalid> wrote:
>>
>http://theory.kitp.ucsb.edu/~paxton/tioga_doc/clas...
>
>i realize it's not totally numerical... but he's thinking along the same
>lines.  fyi.
>

Looks interesting.  Hadn't run across it before.  Maybe Tioga would be a
good
candidate for the graphics component of what I'm talking about.

Phil
unknown (Guest)
on 2006-03-30 11:29
(Received via mailing list)
In article <removed_email_address@domain.invalid>,
M. Edward (Ed) Borasky <removed_email_address@domain.invalid> wrote:
>Phil T. wrote:
>> This has come up before, but maybe we need a Ruby-based DSL that could be aimed
>> squarely at the likes of Matlab and R.
>When you say "we", who are you referring to? People who only know Ruby?
>As far as *I* am concerned, R, Axiom and Maxima are (open source) Domain
>Specific Languages for mathematics. Ruby is a general-purpose
>object-oriented language. Sure, Ruby is good for building
>domain-specific languages, but you just aren't going to reinvent all
>those wheels in Ruby.

Where reinvention would take too long, then perhaps it would be best to
build
bridges.  I guess in my case I'd like to see something that could take
on
Matlab (a closed source product).

Hey, a couple of years ago we were here in this group discussing how
Ruby could
have an impact in the Web Programming arena since it looked like Ruby
was an
also-ran in that area (there were so many tools that were already
dominant in
that space - or so it seemed).  Now Rails has changed all of that.
Scientific
computing/ mathematics is another area where there's room for
innovation.

Maybe we take this proposed tool in a slightly different direction.
Make
it easy to make things web-enabled for example, or maybe make it easy to
use in
a cluster environment (taking advantage of drb).  A LaTeX equation to
RubyMathDSL translator.  Maybe a Rails, RubyMathDSL, LaTeX mashup where
your
'paper' is some sort of interactive document where you can interact with
equations and graphs and maybe something that would make it easier to do
collaborative research where the data in your paper can be referenced by
other interactive papers which can make use of that data in interesting
ways
and add to it and... Ok, it's getting a bit
wierd, I'll admit.  I'm just trying to spark a bit of brainstorming
here...

However that bit about data-sharing between interactive papers seems
kind of
cool.  It would give a whole new meaning to a 'bibliography' and
'references'.
It's not easy right now to share data like that.  When I'm reading
papers I
sometimes think "They spent all this time gathering or generating all of
this
wonderful data and putting it into nice little tables or graphs, but
there's
really no way for me to access it".  Of course this would require a
whole new
infrastructure for publishing papers as web apps, but this is 2006
afterall.

Phil
AlexG (Guest)
on 2006-03-30 11:35
(Received via mailing list)
On Thurs, Mar 30 2006 2:54 pm Ara wrote:
> On Thu, 30 Mar 2006, Phil T. wrote:
> > what are the alternatives to gnuplot?

> you can make production quality graphics using

>   http://theory.kitp.ucsb.edu/~paxton/tioga.html

Wow. That's nice. Really nice! Thanks for the link - not seen it
before.

For those following along at home. I have extended my toy R/Ruby bridge
to be able to directly call any R function with any number of
parameters (as long as they're Integers, Floats or Strings!). Or any
function at all via parse(eval("cmd")). The next step is getting some
conversion code for arrays, etc... And then a lot of tidying up.
Edwin V. (Guest)
on 2006-03-30 15:06
unknown wrote:
> Where reinvention would take too long, then perhaps it would be best to
> build
> bridges.  I guess in my case I'd like to see something that could take
> on
> Matlab (a closed source product).

You can already do a lot of what matlab does with ruby-gsl. There are
also a couple of gnuplot and plotutils bridges to make plotting easy.
(plotutils bridge is part of ruby-gsl)

For an R bridge I use a simple class that writes stuff to a script file
and then executes the script with R. It is far from perfect but doable.
But I would certainly welcome a better bridge.


Edwin
Thomas (Guest)
on 2006-03-30 16:23
(Received via mailing list)
> For an R bridge I use a simple class that writes stuff to a script file
> and then executes the script with R. It is far from perfect but doable.
> But I would certainly welcome a better bridge.

If you're on windows, you could quite easily use ole for accessing R.

thomas
unknown (Guest)
on 2006-03-30 21:29
(Received via mailing list)
In article <removed_email_address@domain.invalid>,
Edwin van Leeuwen  <removed_email_address@domain.invalid> wrote:
>unknown wrote:
>> Where reinvention would take too long, then perhaps it would be best to
>> build
>> bridges.  I guess in my case I'd like to see something that could take
>> on
>> Matlab (a closed source product).
>
>You can already do a lot of what matlab does with ruby-gsl. There are
>also a couple of gnuplot and plotutils bridges to make plotting easy.
>(plotutils bridge is part of ruby-gsl)

True, these things already exist to some degree, but what we need is
something
that's packaged in a very nice way so that someone who doesn't know Ruby
can
download it and start using it without even suspecting that they're
using
Ruby... then as they're getting hooked on it they can start learning
Ruby
without even suspecting.  Another gateway product, kind of like Rails
;-)

Phil
M. Edward (Ed) Borasky (Guest)
on 2006-03-31 09:22
(Received via mailing list)
Edwin van Leeuwen wrote:
> also a couple of gnuplot and plotutils bridges to make plotting easy.
> (plotutils bridge is part of ruby-gsl)
>
> For an R bridge I use a simple class that writes stuff to a script file
> and then executes the script with R. It is far from perfect but doable.
> But I would certainly welcome a better bridge.
>
Both Octave and SciLab are more or less "Matlab clones", if that's what
you want. Octave is open source. SciLab is "free as in beer" and may
have loosened their license a bit since I last looked.

A few years ago, I looked at a lot of open source math and graphics
packages for inclusion in a project. The candidates were Octave,
Xlisp-Stat, Perl Data Language (PerlDL or PDL) and R. When the smoke
cleared, I picked R, even though at the time I was primarily a Perl
programmer.

That project is still going strong today, as is R and its community. I
don't think XLisp-Stat has released anything since then, Octave is more
or less stagnant and I lost track of PerlDL -- it's still the package of
choice for things like astronomical image processing.

--
M. Edward (Ed) Borasky

http://linuxcapacityplanning.com
M. Edward (Ed) Borasky (Guest)
on 2006-03-31 09:22
(Received via mailing list)
Yeah ... in fact there is an R DCOM server and client in the CRAN
package repository for Windows. There's also an interesting package
called, IIRC, RExcel, which lets you call an R server inside an Excel
spreadsheet.

Thomas wrote:
>> For an R bridge I use a simple class that writes stuff to a script
>> file and then executes the script with R. It is far from perfect but
>> doable. But I would certainly welcome a better bridge.
>
> If you're on windows, you could quite easily use ole for accessing R.
>
> thomas
>
>
>

--
M. Edward (Ed) Borasky

http://linuxcapacityplanning.com
M. Edward (Ed) Borasky (Guest)
on 2006-03-31 09:28
(Received via mailing list)
Phil T. wrote:
> True, these things already exist to some degree, but what we need is something
> that's packaged in a very nice way so that someone who doesn't know Ruby can
> download it and start using it without even suspecting that they're using
> Ruby... then as they're getting hooked on it they can start learning Ruby
> without even suspecting.  Another gateway product, kind of like Rails ;-)
>
Hmmm ... the R folks say the same thing about R. :) Download it, load
the R Commander GUI front end, look at the R code it's executing when it
does your analysis, learn to program in R slowly ...

Actually, I really like the Ruby Matrix/Mathn/Rational/Complex setup.
It's a little on the slow side for large problems, but for small
problems, it gets exact answers in a lot of cases and the code is quite
instructive.

--
M. Edward (Ed) Borasky

http://linuxcapacityplanning.com
Meino Christian C. (Guest)
on 2006-03-31 09:37
(Received via mailing list)
Hi,

 Is there a ruby-like way to make an hash from two equeal sized
 arrays, one containing the keys only and the other the values ?

 Thanks a lot for any help in advance !


 Ruby!
 mcc
Hal F. (Guest)
on 2006-03-31 09:40
(Received via mailing list)
Meino Christian C. wrote:
> Hi,
>
>  Is there a ruby-like way to make an hash from two equeal sized
>  arrays, one containing the keys only and the other the values ?
>
>  Thanks a lot for any help in advance !
>

Would this work?

   Hash[*arr1.zip(arr2)]


Hal
Meino Christian C. (Guest)
on 2006-03-31 09:56
(Received via mailing list)
From: Hal F. <removed_email_address@domain.invalid>
Subject: Re: Most simple way to do "array + array => hash"
Date: Fri, 31 Mar 2006 14:39:58 +0900

Hi Hal,

 no, it seems not (from my irb):

 >> a=("A".."Z").to_a
 => ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
 >> b=("A".."Z").sort_by{rand}
 => ["D", "C", "X", "P", "Y", "R", "S", "W", "N", "L", "U", "A", "Z",
"B", "M", "T", "O", "K", "Q", "G", "I", "F", "J", "H", "V", "E"]
 >> c=Hash[*a.zip(b)]
 => {["E", "Y"]=>["F", "R"], ["Q", "O"]=>["R", "K"], ["A", "D"]=>["B",
"C"], ["W", "J"]=>["X", "H"], ["M", "Z"]=>["N", "B"], ["U", "I"]=>["V",
"F"], ["C", "X"]=>["D", "P"], ["I", "N"]=>["J", "L"], ["O", "M"]=>["P",
"T"], ["G", "S"]=>["H", "W"], ["Y", "V"]=>["Z", "E"], ["S", "Q"]=>["T",
"G"], ["K", "U"]=>["L", "A"]}

 by the way: Is there a howto, a piece of text or a webpage or...
 which describes all these "nice little things" which are possible
 in ruby in a single line of code and which occupy the half of a page
 of cryptic code in other languages ?

 Especially ruby newbies like me often fall over such "little things"
like
 converting a "A" to a 65, cause doing "A"[0] is very simple but not
 what a long year C-coder would exspect at first hand.

 Ruby!
 mcc
Kelly Norton (Guest)
on 2006-03-31 10:02
(Received via mailing list)
Hal F. wrote:
>
>   Hash[*arr1.zip(arr2)]
>
>
> Hal
>

I think a flatten is in needed as well:

Hash[*keys.zip(vals).flatten]

/kel
Hal F. (Guest)
on 2006-03-31 10:05
(Received via mailing list)
Meino Christian C. wrote:
> From: Hal F. <removed_email_address@domain.invalid>
> Subject: Re: Most simple way to do "array + array => hash"
> Date: Fri, 31 Mar 2006 14:39:58 +0900
>
> Hi Hal,
>
>  no, it seems not (from my irb):

Oops, forgot to flatten.

    Hash[*a.zip(b).flatten]

irb(main):004:0> Hash[*a.zip(b).flatten]
=> {"V"=>"V", "K"=>"U", "W"=>"S", "L"=>"K", "A"=>"Z", "X"=>"P",
"M"=>"R", "B"=>"X",
"Y"=>"Q", "N"=>"A", "C"=>"M", "Z"=>"O", "O"=>"B", "D"=>"I", "P"=>"H",
"E"=>"D",
"Q"=>"E", "F"=>"G", "R"=>"W", "G"=>"F", "S"=>"N", "H"=>"L", "T"=>"Y",
"I"=>"T",
"U"=>"C", "J"=>"J"}


Cheers,
Hal
rickhg12hs (Guest)
on 2006-03-31 11:40
(Received via mailing list)
> "little things" like converting a "A" to a 65

Here's another way:

$ ruby -w -e 'puts ?A'
65
Dave B. (Guest)
on 2006-03-31 13:09
(Received via mailing list)
On 31 Mar 2006, at 08:38, rickhg12hs wrote:

>> "little things" like converting a "A" to a 65
>
> Here's another way:
>
> $ ruby -w -e 'puts ?A'
> 65

I have always used 'A'[0] to return the ASCII value of A and as
rickhg12hs  wrote ?A will do the same thing.  I cannot find any
reference to this in Pickaxe so can anyone explain how this works?

Thanks,

Dave.
Pit C. (Guest)
on 2006-03-31 15:18
(Received via mailing list)
Dave B. schrieb:
> I have always used 'A'[0] to return the ASCII value of A and as
> rickhg12hs  wrote ?A will do the same thing.  I cannot find any
> reference to this in Pickaxe so can anyone explain how this works?

Page 50 (first edition) or here:

   http://phrogz.net/ProgrammingRuby/tut_stdtypes.html#numbers

Regards,
Pit
unknown (Guest)
on 2006-04-01 02:30
(Received via mailing list)
Very cute.  I hopped onto irb to check out what each step of
Hash[*keys.zip(vals).flatten] does (I'd not used zip nor flatten in
some time) and I can't quite figure out what  function asterisk, ie,
*keys performs (leaving it out or executing *keys.zip(vals).flatten
results in a ruby error and I don't see an explanation of this use of *
in the nutshell book.)  Can one of you enlighten me?

Thx!

Ken
Matthew M. (Guest)
on 2006-04-01 02:42
(Received via mailing list)
> def foo(a, b, c)
>   "a:#{a}  b:#{b}  c:#{c}"
> end
=> nil

> foo(1, 2, 3)
=> "a:1  b:2  c:3"

> x = [4, 5, 6]
=> [4, 5, 6]

> foo(x)
ArgumentError: wrong number of arguments (1 for 3)

> foo(*x)
=> "a:4  b:5  c:6"
Logan C. (Guest)
on 2006-04-01 03:28
(Received via mailing list)
On Mar 31, 2006, at 5:28 PM, removed_email_address@domain.invalid wrote:

> I can't quite figure out what  function asterisk, ie,
> *keys performs (leaving it out or executing *keys.zip(vals).flatten
> results in a ruby error and I don't see an explanation of this use
> of *
> in the nutshell book.)  Can one of you enlighten me?

* is the unary unarray operator

basically what it does  delete the square brackets:

f(*[1,2,3]) -> f(1,2,3)
AlexG (Guest)
on 2006-04-05 11:14
(Received via mailing list)
Hi,

There is a version of my R embedding code at:

http://web.kuicr.kyoto-u.ac.jp/~alexg/files

This is very very rough and early. Think pre-pre-alpha. It is a
conversion of the core part of RSPerl allowing you to call R functions
from ruby scripts and pass simple data between them. Simple data means:
integers, floats and strings and arrays and hashes thereof. So you can
write:

require 'rsruby'
r=RSRuby.new #initialize embedded R interpreter
samples = 100
normal = r.rnorm(samples) #Calls R function 'rnorm'
puts normal #a Ruby array of floats converted from R vector
r.plot({ 'x' => normal,          #You can call with named args
         'xlab' => 'Hello Ruby',
	 'ylab' => 'Ruby ga suki desu'})
sleep(4) #Ooooh - pretty!

At this level it doesn't provide anything that Gordon/Ed's suggestion
of using an external R interpreter couldn't do. But perhaps with a bit
more work it might.

Anyway, if you're interested feel free to download and test. I've tried
it on a linux box and OSX, I have no idea how to build it under Windows
(or even if you could!). Documentation is essentially non-existent at
this stage, though there's a README which tells you how to build - you
just need to tell extconf.rb where your libR sits.

You can find my email address for bug reports/contributions in the
DESCRIPTION file.

Cheers,

AlexG
This topic is locked and can not be replied to.