From: Cliff Rosson Date: 2013-03-05T14:25:50+09:00 Subject: Re: Extending Ruby. Little help or guidance if you are willing! --089e01175e3dc9e30004d726b467 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable So I am slowly stumbling my way through this. I know I've got quite a bit to learn. I've tried to do some reading but some of this is a bit over my head. I'm getting close. I built the below method by reading and copying items from the ruby API docs. My ultimate goal is to extract keys from a hash and compare them. Since I don't know how to compare symbols in C I thought I would start there. require 'inline' module Whatever inline(:C) do |builder| builder.c ' VALUE compare(VALUE sym) { ID id =3D rb_intern_str(rb_str_new2("test")); if (id =3D=3D sym) return Qtrue; return Qfalse; } ' end end if __FILE__ =3D=3D $0 include Whatever puts compare(:test) end This is my output puts compare("tes") =3D> false puts compare("test") =3D> false puts compare(:test) =3D> false Any clues as to why :test fails? On Sun, Mar 3, 2013 at 7:34 PM, Saji Hameed wrote: > Hi Cliff, > > Maybe my ruby binding to the SOMPAK code may serve as a small example... > > https://github.com/sajinh/SOMPAK_4R/tree/master/sompak_4r > > However, I noted that you are already using a ruby binding to the > C-library (WiringPi)!? > > cheers, > saji > > > On Mon, Mar 4, 2013 at 1:52 AM, Cliff Rosson wrot= e: > >> Thanks Quintus. I'll get around to playing with this today. >> >> >> On Sun, Mar 3, 2013 at 3:07 AM, Quintus wrote: >> >>> Am Sun, 3 Mar 2013 10:31:25 +0900 >>> schrieb Cliff Rosson : >>> >>> > Hi Folk, >>> >>> Hi Cliff, >>> >>> > def display_segments(num, display, io, delay =3D 0.005) >>> > # ... >>> > sleep delay >>> > # ... >>> > end >>> >>> I doubt Ruby=92s sleep is exact enough to sleep such a small delay of >>> time, especially given the general Floating-Point precision problems. >>> I=92d suspect that your #sleep call is actually taking longer than you >>> expect. >>> >>> > I think the logic in my C method would be the same as my ruby one. >>> > The part where I run into trouble is converting values. For example I >>> > can't figure out how to decide which pins to light by hash key. In >>> > fact I can't even figure out how to get the hash keys... >>> >>> There are two great sources I use when writing C extensions for Ruby: >>> >>> * The official README.EXT by Matz: >>> >>> http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/tags/v2_0_0_0/README.EXT?vi= ew=3Dmarkup >>> >>> * Rubinius=92 ruby.h header file, because it=92s way better documented = than >>> Ruby=92s: >>> >>> https://github.com/rubinius/rubinius/blob/master/vm/capi/19/include/rub= y/ruby.h >>> >>> For getting values out of hashes, you want rb_hash_aref(). >>> >>> Vale, >>> Marvin >>> >> >> >> >> -- >> vizualize.me/cliffrosson >> > > > > -- > > Saji N Hameed, > ARC-ENV, Center for Advanced Information Science and Technology, > University of Aizu, Tsuruga, Ikki-machi, > Aizuwakamatsu-shi, Fukushima 965-8580, > Japan > > Tel: +81242 37-2736 > Fax:+81242 37-2760 > email: saji@u-aizu.ac.jp > url: http://www.u-aizu.ac.jp > bib: http://www.researcherid.com/rid/B-9188-2009 > --=20 vizualize.me/cliffrosson --089e01175e3dc9e30004d726b467 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable
So I am slowly stumbling my way through this. I know I'= ;ve got quite a bit to learn. I've tried to do some reading but some of= this is a bit over my head. I'm getting close. I built the below metho= d by reading and copying items from the ruby API docs. My ultimate goal is = to extract keys from a hash and compare them. Since I don't know how to= compare symbols in C I thought I would start there.


require 'inline'
module Whatever

=A0 inline(:C) do |builder|
=
=A0 =A0 builder.c '
=A0 =A0 VALUE compare(VALUE sym= ) {

=A0 =A0 =A0 ID id =3D rb_intern_str(rb_str_new2("t= est"));
=A0 =A0 =A0=A0
=A0 =A0 =A0 if (id =3D=3D s= ym) return Qtrue;
=A0 =A0 =A0 return Qfalse;
=A0 =A0 }<= /div>
=A0 =A0 '
=A0 end
end

if __FILE__ =3D=3D $0
<= div>=A0 include Whatever
=A0 =A0puts compare(:test)
end=


This is my output

puts compare("tes")
=3D> = false
puts compare("test")
=3D>= ; false
puts compare(:test)
=3D> false

Any clues as to why :test fails?
=


On Sun, Mar 3, 2013 at 7:34 PM, Saji Hameed <saji@u-aizu.ac.jp><= /span> wrote:


On = Mon, Mar 4, 2013 at 1:52 AM, Cliff Rosson <cliff.rosson@gmail.com= > wrote:
Thanks Quintus. I'll ge= t around to=A0playing=A0with this today.

On Sun, Mar 3, 2013 at 3:07 AM, Quintus <quintus@quintilianus.eu= > wrote:
Am Sun, 3 Mar 2013 10:31:25 +0900
schrieb Cliff Rosson <cliff.rosson@gmail.com>:

> Hi Folk,

Hi Cliff,

> def display_segments(num, display, io, delay =3D 0.005)
> =A0 # ...
> =A0 sleep delay
> =A0 # ...
> end

I doubt Ruby=92s sleep is exact enough to sleep such a small delay of
time, especially given the general Floating-Point precision problems.
I=92d suspect that your #sleep call is actually taking longer than you
expect.

> I think the logic in my C method would be the same as my ruby one.
> The part where I run into trouble is converting values. For example I<= br> > can't figure out how to decide which pins to light by hash key. In=
> fact I can't even figure out how to get the hash keys...

There are two great sources I use when writing C extensions for Ruby:=

* The official README.EXT by Matz:
=A0 http://svn.ruby-lang.org/cgi-bin/= viewvc.cgi/tags/v2_0_0_0/README.EXT?view=3Dmarkup

* Rubinius=92 ruby.h header file, because it=92s way better documented than=
=A0 Ruby=92s:
=A0 https://github.com/rubinius/rubinius= /blob/master/vm/capi/19/include/ruby/ruby.h

For getting values out of hashes, you want rb_hash_aref().

Vale,
Marvin



--



--

Saji N = Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,=
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushim= a 965-8580,
Japan
=A0 =A0 =A0 =A0
Tel: +81242 37-2736
Fax:+81242 37-27= 60
=A0email: = saji@u-aizu.ac.jp
=A0url: http://www.u-= aizu.ac.jp
=A0bib: http://www.researcherid.com/rid/B-9188-2009



--
--089e01175e3dc9e30004d726b467--