quick question about how array objects are handled

C

Chance Dinkins

Btw, thanks in advance for any help - this community seems great!

I'm using the following line to try to remove objects from an array that
begin with .
_array.each{|item| item.gsub(/^[\.][.]*/){|match|
_array.delete(match)}}

and although the match is functioning properly, the _array still
contains the values after being deleted. I've wondered if
Array.delete("param") actually returns the resulting array but that
doesnt seem to be the case. How do I go about actually altering the
state of the current array if the above is not done correctly?

Thanks for your help!
 
T

Teleolurian

I don't think there's an in place editor for delete_if, so you'd want

_array = _array.delete_if {|x| x.match(/^\./)}
 
C

Chance Dinkins

Thanks guys, I apperciate it - these.. dynamic methods (is that what
they are considered?) are a little strange to me.
 
A

ara.t.howard

Thanks guys, I apperciate it - these.. dynamic methods (is that what
they are considered?) are a little strange to me.

they are normal functions but one's which take anonymous functions
(blocks) as arguments. ruby makes this very easy on the eyes, but
even C has this concept: do a 'man qsort' and you'll see

void
qsort(void *base, size_t nel, size_t width, int (*compar)(const
void *, const void *));

note the 'compar' function you can pass in. so, in c, you have to
define that function up front and then pass a pointer to it. in ruby
you can do things like

compar = lambda{|a,b| a <=> b}

array.qsort &compar

or, more compactly

array.qsort{|a,b| a <=> b}

of course the method is called 'sort' and not 'qsort', but the
principle is exactly the same.


kind regards.

a @ http://codeforpeople.com/
 
D

David A. Black

Hi --

I don't think there's an in place editor for delete_if, so you'd want

_array = _array.delete_if {|x| x.match(/^\./)}

delete_if operates on the array in place:

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.delete_if {|e| e == 1 }
=> [2, 3]
irb(main):003:0> a
=> [2, 3]


David
 
D

David A. Black

Hi --

Thanks guys, I apperciate it - these.. dynamic methods (is that what
they are considered?) are a little strange to me.

The methods that expect a code block as part of the method call are
iterators. Basically the code block is a piece of executable code that
you're making available to be called from the method itself.
Typically, that means that a method will fire the code block once for
each element in a collection object like an array.


David
 
D

David A. Black

Hi --

they are normal functions but one's which take anonymous functions (blocks)
as arguments. ruby makes this very easy on the eyes, but even C has this
concept: do a 'man qsort' and you'll see

void
qsort(void *base, size_t nel, size_t width, int (*compar)(const void *,
const void *));

note the 'compar' function you can pass in. so, in c, you have to define
that function up front and then pass a pointer to it. in ruby you can do
things like

compar = lambda{|a,b| a <=> b}

array.qsort &compar

or, more compactly

array.qsort{|a,b| a <=> b}

of course the method is called 'sort' and not 'qsort', but the principle is
exactly the same.

I'd leave room, though, for the fact that in Ruby, you can provide a
code block to a method, as part of the syntax of the method call, but
you can also send anonymous functions in the argument list. So there's
a sense of "The Block", so to speak, separate from any functions you
might include as regular arguments. (Yes, I do know about the &block
idiom in method signatures :)


David
 
R

Robert Klemme

The methods that expect a code block as part of the method call are
iterators. Basically the code block is a piece of executable code that
you're making available to be called from the method itself.
Typically, that means that a method will fire the code block once for
each element in a collection object like an array.

Yes, but not in all cases. The presence of the block does not tell
you anything about the frequency of invocation. Just think about
File.open for example. So I'd say the general statement "The methods
that expect a code block as part of the method call are iterators" is
misleading.

Kind regards

robert
 
R

Robert Klemme

I don't think there's an in place editor for delete_if, so you'd want

_array = _array.delete_if {|x| x.match(/^\./)}

You could as well do

_array.delete_if {|s| s[0] == ?.}

Cheers

robert
 
D

David A. Black

Hi --

Yes, but not in all cases. The presence of the block does not tell
you anything about the frequency of invocation. Just think about
File.open for example. So I'd say the general statement "The methods
that expect a code block as part of the method call are iterators" is
misleading.

I said "typically" :) One lesson at a time....


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top