[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] nfc 1.0.0 Released

Aaron Patterson

6/2/2009 5:22:00 AM

nfc version 1.0.0 has been released!

* <http://seattlerb.rubyfor...

NFC is a ruby wrapper for the Near Field Communication library. The Near
Field Communication library works with many USB RFID readers, so this gem
lets you read RFID tags.

## FEATURES/PROBLEMS:

* Only supports ISO1443A (MIFARE) tags right now.

## SYNOPSIS:

require 'nfc'

# Read your tag and print the info
p NFC.instance.find

## REQUIREMENTS:

* A USB RFID reader. I'm using the touchatag[http://tou...].
* ffi
* libnfc

## INSTALL:

* First install libnfc[http://l...]
* Make sure libnfc.dylib or libnfc.so is in your library path
* gem install nfc

Changes:

### 1.0.0 / 2009-06-01

* 1 major enhancement

* Birthday!

* <http://seattlerb.rubyfor...

--
Aaron Patterson
http://tenderlovem...

9 Answers

Suraj Kurapati

6/2/2009 5:02:00 PM

0

Aaron Patterson wrote:
> require 'nfc'
>
> # Read your tag and print the info
> p NFC.instance.find

Seems like the NFC class includes Singleton, correct?

require 'singleton'
class NFC
include Singleton
# ...
end

You could simplify your public API (by not requiring users to type
instance) like this:

require 'singleton'
NFC = Class.new do
include Singleton
# ...
end.instance
--
Posted via http://www.ruby-....

Brian Candler

6/2/2009 5:16:00 PM

0

Suraj Kurapati wrote:
> You could simplify your public API (by not requiring users to type
> .instance) like this:
>
> require 'singleton'
> NFC = Class.new do
> include Singleton
> # ...
> end.instance

Module functions are good for this too.

module NFC
def find(*args)
puts *args
end
module_function :find
end

NFC.find("123")

class Foo
include NFC
def bar
find("456")
end
end

Foo.new.bar
--
Posted via http://www.ruby-....

Aaron Patterson

6/2/2009 8:31:00 PM

0

On Wed, Jun 03, 2009 at 02:01:45AM +0900, Suraj Kurapati wrote:
> Aaron Patterson wrote:
> > require 'nfc'
> >
> > # Read your tag and print the info
> > p NFC.instance.find
>
> Seems like the NFC class includes Singleton, correct?
>
> require 'singleton'
> class NFC
> include Singleton
> # ...
> end
>
> You could simplify your public API (by not requiring users to type
> .instance) like this:
>
> require 'singleton'
> NFC = Class.new do
> include Singleton
> # ...
> end.instance

That would create a new instance of the object as soon as the gem is
required, and that might not be desired. Especially when dealing with a
piece of hardware.

--
Aaron Patterson
http://tenderlovem...

Suraj Kurapati

6/2/2009 11:18:00 PM

0

Aaron Patterson wrote:
> On Wed, Jun 03, 2009 at 02:01:45AM +0900, Suraj Kurapati wrote:
>> include Singleton
>> end.instance
>
> That would create a new instance of the object as soon as the gem is
> required, and that might not be desired. Especially when dealing with a
> piece of hardware.

In that case, use Kernel#autoload to load your library only when the NFC
constant is actually used:

autoload :NFC, 'nfc'

Then your users can simply write:

NFC.find
--
Posted via http://www.ruby-....

Eric Hodel

6/2/2009 11:30:00 PM

0

On Jun 2, 2009, at 16:18, Suraj Kurapati wrote:
> Aaron Patterson wrote:
>> On Wed, Jun 03, 2009 at 02:01:45AM +0900, Suraj Kurapati wrote:
>>> include Singleton
>>> end.instance
>>
>> That would create a new instance of the object as soon as the gem is
>> required, and that might not be desired. Especially when dealing
>> with a
>> piece of hardware.
>
> In that case, use Kernel#autoload to load your library only when the
> NFC
> constant is actually used:
>
> autoload :NFC, 'nfc'
>
> Then your users can simply write:
>
> NFC.find

You have to require 'nfc' to get the autoload, so this won't work.
Adding two files to do this is far lamer than Singleton.

If you're really dead set on it, why don't just fork it? I'm quite
certain Aaron won't care.

Suraj Kurapati

6/3/2009 4:17:00 AM

0

Eric Hodel wrote:
> You have to require 'nfc' to get the autoload, so this won't work.

I beg to differ; you need not require() before using autoload():

sun@yantram ~/tmp> cat nfc.rb
NFC = Class.new do
require 'singleton'
include Singleton

def find(*args)
p :find => args
end
end.instance

p :loaded_nfc


sun@yantram ~/tmp> cat run_this.rb
autoload :NFC, 'nfc'
NFC.find('123')


sun@yantram ~/tmp> multiruby run_this.rb

VERSION = 1.8.7-p72
CMD = ~/.multiruby/install/1.8.7-p72/bin/ruby run_this.rb

:loaded_nfc
{:find=>["123"]}

RESULT = 0

VERSION = 1.8.6-p368
CMD = ~/.multiruby/install/1.8.6-p368/bin/ruby run_this.rb

:loaded_nfc
{:find=>["123"]}

RESULT = 0

VERSION = 1.9.1-p0
CMD = ~/.multiruby/install/1.9.1-p0/bin/ruby run_this.rb

:loaded_nfc
{:find=>["123"]}

RESULT = 0

VERSION = jruby-1.2.0
CMD = ~/.multiruby/install/jruby-1.2.0/bin/ruby run_this.rb

:loaded_nfc
{:find=>["123"]}

RESULT = 0

TOTAL RESULT = 0 failures out of 4

Passed: 1.8.6-p368, 1.8.7-p72, jruby-1.2.0, 1.9.1-p0
Failed:

> Adding two files to do this is far lamer than Singleton.

Why would you need two files? Because NFC is a gem?

Since RubyGems overrides Kernel#require() and Kernel#autoload() uses
Kernel#require(), the autoload approach should work even if loading NFC
from a gem.
--
Posted via http://www.ruby-....

Ryan Davis

6/3/2009 8:54:00 AM

0


On Jun 2, 2009, at 21:16 , Suraj Kurapati wrote:

> Eric Hodel wrote:
>> You have to require 'nfc' to get the autoload, so this won't work.
>
> I beg to differ; you need not require() before using autoload():

yes... but you have to put that autoload SOMEWHERE. Otherwise, what's
the point? You should have been able to intuit that eric understood
this by stating "adding two files to do this is far lamer than
Singleton".

I believe it is fair to say at this point that nobody involved with
nfc cares about "improving" the API as you're suggesting. There isn't
anything wrong with typing "instance". As Eric stated, if you've got
such a boner for autoload, you're welcome to fork.


Suraj Kurapati

6/3/2009 4:02:00 PM

0

Ryan Davis wrote:
> On Jun 2, 2009, at 21:16 , Suraj Kurapati wrote:
>
>> Eric Hodel wrote:
>>> You have to require 'nfc' to get the autoload, so this won't work.
>>
>> I beg to differ; you need not require() before using autoload():
>
> yes... but you have to put that autoload SOMEWHERE. Otherwise, what's
> the point? You should have been able to intuit that eric understood
> this by stating "adding two files to do this is far lamer than
> Singleton".

Ah, I see now.

> I believe it is fair to say at this point that nobody involved with
> nfc cares about "improving" the API as you're suggesting. There isn't
> anything wrong with typing "instance". As Eric stated,

Alright.

> if you've got such a boner for autoload, you're welcome to fork.

Such harsh words! You could have stopped at the previous sentence; your
point has already been made. This remark only adds hostility; a poison
coating for your arrows.

Please remember MINSWAN. :-(
--
Posted via http://www.ruby-....

Ryan Davis

6/3/2009 8:53:00 PM

0


On Jun 3, 2009, at 09:01 , Suraj Kurapati wrote:

>> if you've got such a boner for autoload, you're welcome to fork.
>
> Such harsh words! You could have stopped at the previous sentence;
> your
> point has already been made. This remark only adds hostility; a
> poison
> coating for your arrows.

I assure you that hostility would be offering you MY boner if you
didn't like the current implementation.