From: Cliff Rosson Date: 2013-03-06T01:31:58+09:00 Subject: Re: Extending Ruby. Little help or guidance if you are willing! --0016e6d27555137b3304d730039e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable > > Honestly, you could probably get really far by changing a few things abou= t > the implementation here. I'm assuming you're calling this frequently, in > which case, you're creating these arrays often, it might be faster to set > them into a constant, which you can then return from the case statement. > Also, the pins.include?(seg) is going to do a sequential search through t= he > arrays. You could possibly run some tests to see which keys are most > frequent and place them at the beginning so they are found first. If you > have control over the display object, you might be able to swap out the > symbols with integers, allowing for you to be clever with bit mathematics= . > It could possibly be faster to use a hash or a set than to use an array f= or > your pins, but not totally confident given the smallness of the arrays. I= n > general, you should benchmark to see if any of these have more merit. Interesting point. I think you are right. I'll try and experiment with this today. On Mon, Mar 4, 2013 at 11:30 PM, Bartosz Dziewo=C5=84ski wrote: > On Tue, 05 Mar 2013 06:25:50 +0100, Cliff Rosson > wrote: > > 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; >> } >> >> ... >> >> >> puts compare("tes") >> =3D> false >> puts compare("test") >> =3D> false >> puts compare(:test) >> =3D> false >> >> Any clues as to why :test fails? >> > > You're comparing an 'ID' value with a 'VALUE' value; these are not the > same. VALUE represents a Ruby object (any object); ID is an internal > representation of a method or variable name. > > You can convert an ID to a VALUE representing the corresponding symbol > using rb_id2name(): > > ID id =3D rb_id2name(rb_intern_str(rb_**str_new2("test"))); > > Then it should work. (Untested.) > > -- > Matma Rex > > --=20 vizualize.me/cliffrosson --0016e6d27555137b3304d730039e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Honestly, you could probably get really far by changing = a few things about the implementation here. I'm assuming you're cal= ling this frequently, in which case, you're creating these arrays often= , it might be faster to set them into a constant, which you can then return= from the case statement. Also, the pins.include?(seg) is going to do a seq= uential search through the arrays. You could possibly run some tests to see= which keys are most frequent and place them at the beginning so they are f= ound first. If you have control over the display object, you might be able = to swap out the symbols with integers, allowing for you to be clever with b= it mathematics. It could possibly be faster to use a hash or a set than to = use an array for your pins, but not totally confident given the smallness o= f the arrays. In general, you should benchmark to see if any of these have = more merit.

Interesting point. I think you are right. I'l= l try and=C2=A0experiment=C2=A0with this today.=C2=A0


On Mon, Mar 4, 2013 at = 11:30 PM, Bartosz Dziewo=C5=84ski <matma.rex@gmail.com> wr= ote:
On Tue, 05 Mar 2013 06:25:= 50 +0100, Cliff Rosson <cliff.rosson@gmail.com> wrote:

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

=C2=A0 =C2=A0 =C2=A0 ID id =3D rb_intern_str(rb_str_new2("test&= quot;));

=C2=A0 =C2=A0 =C2=A0 if (id =3D=3D sym) return Qtrue;
=C2=A0 =C2=A0 =C2=A0 return Qfalse;
=C2=A0 =C2=A0 }

...


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

Any clues as to why :test fails?

You're comparing an 'ID' value with a 'VALUE' value; th= ese are not the same. VALUE represents a Ruby object (any object); ID is an= internal representation of a method or variable name.

You can convert an ID to a VALUE representing the corresponding symbol usin= g rb_id2name():

=C2=A0 =C2=A0 ID id =3D rb_id2name(rb_intern_str(rb_str_new2("t= est")));

Then it should work. (Untested.)

--
Matma Rex




--
--0016e6d27555137b3304d730039e--