ruby padding an array

I'm getting the following error when I run my code from my rspec file:

pad pads element to the end of an array Failure/Error: expect(pad(array,array.length + pad_size, 'apple')).to eq(array+Array.new(pad_size, 'apple')) expected: ["apple", "apple", "apple",...]

my current code is:

def pad!(array, min_size, value = nil) #destructive difference = min_size-array.length difference.times {array << value} array end def pad(array, min_size, value = nil) #non-destructive difference = min_size-array.length difference.times {array << value} array.clone end

Can anyone help me figure out why it's giving me this error message? I thought that {array << value } was adding new elements to the end of my array inputs. Thank you!

--------------Solutions-------------

Your non-destructive version is actually destructive. What you want is probably this:

def pad(array, min_size, value = nil)
pad!(array.clone, min_size, value)
end

There's no point in implementing the same behaviour twice. Make one bridge off of the other.

Category:ruby Time:2017-12-05 Views:2
Tags: ruby padding

Related post

  • Pad an array to be a certain size 2011-04-10

    There's probably a more efficient and more Ruby-ish way to do this: # Pad array to size n by adding x's. Don't do anything if n <= a.length. def padleft(a, n, x) return a if n <= a.length return padleft([x] + a, n, x) end What would you suggest

  • PHP: Changing or padding an array's key names 2010-03-02

    I have the following array: $person = array('first_name' => 'Fred', 'last_name' => 'Flintstone'); I want to change/pad the keys so the array will end up as (note extra colons): $person = array('::first_name::' => 'Fred', '::last_name::' =

  • I want to read an entire MySQL table into ruby as an array. I am using active record 2011-08-02

    I want to read an entire MySQL table (or table column) into ruby as an array. I am using active record, ruby 1.9, rails 3 and mysql 5.5. How can I do this? --------------Solutions------------- If you just want raw data then you can use connection.sel

  • diff a ruby string or array 2008-09-17

    Is there a ruby library that will take two strings or two arrays and return the difference between the two strings/arrays? --------------Solutions------------- diff.rb is what you want, which is available at http://users.cybercity.dk/~dsl8950/ruby/di

  • (Ruby) If the array intersection operator ( & ) is inefficient, why is it available? 2009-03-31

    I asked a question yesterday about comparing ranges for overlap and its been stuck in my throat ever since. The consensus seems to be that my preferred answer which involves using the array intersection operator (&), is inefficient because compar

  • Ruby - test for array 2009-10-06

    What is the right way to: is_array("something") # => false (or 1) is_array(["something", "else"]) # => true (or > 1) or to get the count of items in it? --------------Solutions------------- You probably want to use kind_of?(). >> s = "

  • Ruby: Comparing two Arrays of Hashes 2009-10-15

    I'm definitely a newbie to ruby (and using 1.9.1), so any help is appreciated. Everything I've learned about Ruby has been from using google. I'm trying to compare two arrays of hashes and due to the sizes, it's taking way to long and flirts with run

  • Ruby two dimensional array: finding an object's coordinates 2009-11-18

    Assume, I have a two dimensional array A, and it's stated that somewhere inside it there's an object my_element. What's the quickest way to find out its coordinates? I am using Ruby 1.8.6. --------------Solutions------------- This is one way. I'm not

  • ruby working on array elements in groups of four 2010-01-01

    I have a ruby script array when each element needs processing : threads = [] elemets.each do |element| threads.push(Thread.new{process(element)}} end threads.each { |aThread| aThread.join } how ever due to resource limitations, the script works in an

  • Ruby: After inserting arrays into a list, how can I retrieve them and process them as arrays? 2010-03-06

    Here's what I'm trying to do: val1 = [26, 27, 24, 25, 29, 28] val2 = [17, 20, 22, 21] val3 = [36, 33, 31, 29] val4 = [20, 18, 17, 22, 21, 23] vals = {val1, val2, val3, val4} sum = 0 count = 0 vals.each do |val| for i in 0..val.size-1 do #sum += val[i

  • Ruby: Why is Array.sort slow for large objects? 2010-03-13

    A colleague needed to sort an array of ActiveRecord objects in a Rails app. He tried the obvious Array.sort! but it seemed surprisingly slow, taking 32s for an array of 3700 objects. So just in case it was these big fat objects slowing things down, h

  • ruby parseexecel gem - array not implemented 2010-04-27

    I am trying to work with two worksheets at the same time. So I have code require 'parseexcel' #Open the excel file passed in from the commandline workbook = Spreadsheet::ParseExcel.parse(ARGV[0]) workbook2 = Spreadsheet::ParseExcel.parse(ARGV[1]) #Ge

  • Ruby koan - about arrays - test_accessing_array_elements array[5,0] 2010-07-21

    Possible Duplicate: Why does array.slice behave differently for (length, n) In Ruby koan "about_arrays.rb", test_accessing_array_elements there's two similar start/length slice statements accessing parts of an array. Ref extract below. Both should be

  • Ruby - return an array in random order 2010-08-12

    What is the easiest way to return an array in random order in Ruby? Anything that is nice and short that can be used in an IRB session like [1,2,3,4,5].random() # or random_sort([1,2,3,4,5]) --------------Solutions------------- If you don't have [].s

  • Ruby 1.9 Array.to_s behaves differently? 2010-10-18

    i wrote a quick little application that takes a base file of code with some keywords, a file of replacements for the keywords, and outputs a new file with the keywords replaced. When i was using Ruby 1.8, my outputs would look fine. Now when using Ru

  • Ruby combining an array into one string 2010-10-25

    In Ruby is there a way to combine all array elements into one string? Example Array: @arr = ['<p>Hello World</p>', '<p>This is a test</p>'] Example Output: <p>Hello World</p><p>This is a test</p> Thanks

  • Ruby/ RoR - Looping array of objects against an associative array 2010-12-20

    I h've an array of object of Questions @questions --- - !ruby/object:Question attributes: id: "1" answer: "2" - !ruby/object:Question attributes: id: "7" answer: "1" - !ruby/object:Question attributes: id: "6" answer: "4" - !ruby/object:Question attr

  • In Ruby, why does Array.new(size, object) create an array consisting of multiple references to the same object? 2011-01-09

    As mentioned in this answer, Array.new(size, object) creates an array with size references to the same object. hash = Hash.new a = Array.new(2, hash) a[0]['cat'] = 'feline' a # => [{"cat"=>"feline"},{"cat"=>"feline"}] a[1]['cat'] = 'Felix' a

  • Perl to Ruby conversion (multidimensional arrays) 2011-03-08

    I'm just trying to get my head around a multidimensional array creation from a perl script i'm currently converting to Ruby, I have 0 experience in Perl, as in i opened my first Perl script this morning. Here is the original loop: my $tl = {}; for my

  • Does ruby have the array method select! or not? 2011-03-11

    > a = %w(a b c) => ["a", "b", "c"] > a.select!{|e| !e.nil?} NoMethodError: undefined method `select!' for ["a", "b", "c"]:Array from (irb):2 ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.5.0], MBARI 0x6770, Ruby Enterprise Edition 20

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.876 (s). 13 q(s)