The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
NUM2LONG strangeness

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
NUM2LONG strangeness Posted: Nov 3, 2005 9:38 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: NUM2LONG strangeness
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
While attempting to refactor array.c a little bit, I noticed that the rb_ary_ref() function, the function behind the Array#[] instance method, was performing explicit SYMBOL_P checks. I thought this was odd, because NUM2LONG already has a builtin type check, right?

One little problem - NUM2LONG doesn't seem to mind symbols. If you pass a symbol object to NUM2LONG, it just returns any ol' random number!

#include "ruby.h"
#include <stdio.h>

static VALUE foo_test(VALUE self, VALUE num)
{
   long x;
   x = NUM2LONG(num);

   printf("NUM: %ld\n", x);

   return Qnil;
}

void Init_foo(){
   VALUE cFoo = rb_define_class("Foo", rb_cObject);
   rb_define_method(cFoo, "test", foo_test, 1);
}

# test.rb
f = Foo.new
f.test("foo".to_sym) # 10201! Yikes!


Oofda. Off to the mailing list. Well, knowing this has helped me write better unit tests, at least.

Read: NUM2LONG strangeness

Topic: Keeping the Code Organic Previous Topic   Next Topic Topic: Migrating from MySQL to PostgreSQL in 60 seconds (with Rails)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use