Using each_slice possible?
-
I have a feeling this is a stupid question but I can't find the answer!
I need to use each_slice in a plugin I'm working on but can't get it to work... I keep getting Error: #<NoMethodError: undefined method `each_slice' for #Array:0xc6c8c14>.
Thanks in advance!
Frank
-
Thanks Dan!
But I get this error now...
Error: #<NoMethodError: undefined method `abs' for Math:Module> -
@frankn said:
But I get this error now...
Error: #<NoMethodError: undefined method `abs' for Math:Module>Fixed absolute call (in example above.)
It is not a
Math
module function. It is a standard RubyNumeric
instance method.I just assumed the
Math
module would have an "abs" function.
The absolute call is to prevent an Index error:
ary.slice!(0,-3) %(#004000)[Error: #<IndexError: (eval):0:in
slice!': negative length (-3)>]` -
Thanks Dan that works awesome!
Now I'm having the same problem with delete_if! Isn't there an addon or file we can download to get the missing methods? If not how do you know figure out how to make new ones as you did? I don't want to have to bother anyone the next time I get a similar error if I can fix it myself.
-
@frankn said:
Now I'm having the same problem with "delete_if!"
That is because the method's name is
delete_if
.There is no exclamation point name.
You should refer to the documentation for Ruby v1.8.6 when programming for Ruby v1.8.6.
RUBY PROGRAMMING REFERENCES -
@dan rathbun said:
@frankn said:
Now I'm having the same problem with "delete_if!"
That is because the method's name is
delete_if
.There is no exclamation point name.
I was actually using the ! as punctuation not in my code.
BUT reading your post made me realize I had made typo somewhere else in my code causing the error.
Thanks again for your help sir.
-
@frankn said:
Isn't there an addon or file we can download to get the missing methods?
There may be a "Backport" gem for Ruby v1.8.6 that adds later methods.
But you would need to convince all users to install it. It also may not have ALL the new methods in later versions (such as v1.9.x or v2.0.x.)
@frankn said:
If not how do you know figure out how to make new ones as you did?
I do not know how to answer that. Many many years experience.
Read as many books on Ruby as you can.
Keeping trying and failing, until you gain success ?
-
Here is another edition that will collect the slices into an output array, IF a block is NOT given. (Sort of like the opposite to
flatten()
.)If a block is given, it returns an array of the block's return values.
Prints to
$stdout
only if$VERBOSE == true
.def slicer(ary,num) a2 = ary.dup len = num.to_i.abs out = [] until (s = a2.slice!(0,len)).empty? puts(s.inspect) if $VERBOSE if block_given? out << yield(s) puts( out.last.inspect ) if $VERBOSE else out << s end end # until return out end # slicer()
EDIT: fixed puts'ing block results
-
EDIT: fixed puts'ing block results in the 2nd example.
-
You get a
NoMethodError
becauseEnumerable
does not have that method defined in Ruby 1.8.x.This should do the same, except you pass the array in as the 1st argument:
def slicer(ary,num) a2 = ary.dup len = num.to_i.abs until (s = a2.slice!(0,len)).empty? if block_given? yield(s) else puts(s.inspect) end end # until end # slicer()
See another example that will collect the slices into an output array, IF a block is NOT given. (Sort of like the opposite to
flatten()
.) If a block is given, it returns an array of the block's return values. Prints to$stdout
only if$VERBOSE == true
.Fixed absolute call. It is not a
Math
module function. It is a standard RubyNumeric
instance method.
Advertisement