ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: Help convert a Perl user to the Ruby Way.
by Robert Dober other posts by this author
Nov 12 2006 9:45AM messages near this date
Re: Help convert a Perl user to the Ruby Way. | Re: Help convert a Perl user to the Ruby Way.
On 11/12/06, David Vallner <david@[...].net>  wrote:
> 
>  Sebastian Reid wrote:
>  > Hi all.
>  >
>  >     I've been working on some Ruby projects using the Rails framework
>  > and getting on well, however recently I've started on some projects
>  > using Ruby alone and have run into some issues as the task gets a little
>  > more complicated.
>  >
>  >     One particular script is giving me trouble and I suspect it is due
>  > to my Perly ways.  Below is a pastebin of the code.  As some I'm sure
>  > can tell, it is an rbot plugin that right now doesn't do a whole lot.
>  >
>  > http://pastebin.com/822611
>  >
>  >     The highlighted line is flagging up the error:
>  >
>  > TypeError: no implicit conversion from nil to integer
>  >
>  >     on line 18 (highlighted).
>  >
>  >     Now this is looking to me like a variable instantiation issue which
>  > brings us back to Perl.  In Perl that line would be perfectly legal
>  > (though some would debate its elegance) and hashes and arrays would be
>  > created as and when required.
>  >
> 
>  In Ruby, only instance variables are autovivified to nil, and that by
>  itself is a behaviour that gets on my nerves (typo-prone).


agreed but see JEGII remark about warnings



From the code it looks @registry should be an object (maybe use a
>  Struct) instead of a Hash, and you should preinitialise its contents to
>  empty arrays and whatnot in its initialize method.


501/2 >  ruby -e 'puts Array.new[nil]'
-e:1:in `[]': no implicit conversion from nil to integer (TypeError)
        from -e:1
Given that I rather think  that
(a) @registry["memory"] is referencing an Array object
and
(b) @registry["context"][1] evaluates to nil

That was the question OP, or was it not?
Now if you really want to shoot yourself into your leg here is the gun:

class  GunArray < Array
       def [] *args, &block
            super(*args,&block) rescue nil end
       end
end


you could also monkeypatch Array but I am too lazy to look if its
method_alias or alias_method ;)

> 
>  >     I'm willing to accept that this may not be the best way to do things
>  > in Ruby, thus I come to you to ask for a quick rundown on best practices
>  > in these situations.
>  >
>  >     I've played around with default procs and the like in the
>  > initialisation routine to no avail.
> 
>  Using blocks to initialize hashes / arrays is fine when not nested,
>  otherwise things get messy. Clean up your data structure initialisation
>  to be explicit?


That is definitely my advice too, but I am less strict ;)

David Vallner
> 
> 
> 
>  Cheers
Robert


-- 
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernard Shaw
Thread:
Sebastian Reid
dblack
Paul Lutus
Paul Lutus
Phrogz
Tom Pollard
Xavier Noria
Tom Pollard
Sebastian Reid
David Vallner
Robert Dober
Sebastian Reid
James Edward Gray II

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved