Arrays

Hello
How could I efficiently do Array.chomp for removing only nils at the end?

(This would be a nice addifional feature!)
Thank you
Berg

You could try this:

irb(main):012:0> a=['one',3.14, 5, nil, nil, nil]
=> ["one", 3.14, 5, nil, nil, nil]

irb(main):016:0* def array_chomp(arr)
irb(main):017:1> i = arr.rindex {|v| v != nil}
irb(main):018:1> arr[0, i+1]
irb(main):019:1> end

irb(main):021:0> array_chomp a
=> ["one", 3.14, 5]

···

On Sat, Apr 2, 2016 at 7:03 PM, A Berger <aberger7890@gmail.com> wrote:

Hello
How could I efficiently do Array.chomp for removing only nils at the end?

(This would be a nice addifional feature!)
Thank you
Berg

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

['one',3.14, 5, nil, nil, nil].compact # => ["one", 3.14, 5]

···

3 апр. 2016 г. 4:24 пользователь "Rajinder Yadav" <devguy.ca@gmail.com> написал:

You could try this:

irb(main):012:0> a=['one',3.14, 5, nil, nil, nil]
=> ["one", 3.14, 5, nil, nil, nil]

irb(main):016:0* def array_chomp(arr)
irb(main):017:1> i = arr.rindex {|v| v != nil}
irb(main):018:1> arr[0, i+1]
irb(main):019:1> end

irb(main):021:0> array_chomp a
=> ["one", 3.14, 5]

On Sat, Apr 2, 2016 at 7:03 PM, A Berger <aberger7890@gmail.com> wrote:
> Hello
> How could I efficiently do Array.chomp for removing only nils at the end?
>
> (This would be a nice addifional feature!)
> Thank you
> Berg
>
>
>
> Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;
>

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

you should search for "ruby array removing only nils at the end"
and pick a solution e.g. from

···

Am 03.04.2016 um 01:03 schrieb A Berger:

How could I efficiently do Array.chomp for removing only nils at the end

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Sorry, not correct example. :slight_smile: Not at the end ...

···

3 апр. 2016 г. 8:46 пользователь "Evgeniy Shurmin" <eshurmin@gmail.com> написал:

['one',3.14, 5, nil, nil, nil].compact # => ["one", 3.14, 5]
3 апр. 2016 г. 4:24 пользователь "Rajinder Yadav" <devguy.ca@gmail.com> > написал:

You could try this:

irb(main):012:0> a=['one',3.14, 5, nil, nil, nil]
=> ["one", 3.14, 5, nil, nil, nil]

irb(main):016:0* def array_chomp(arr)
irb(main):017:1> i = arr.rindex {|v| v != nil}
irb(main):018:1> arr[0, i+1]
irb(main):019:1> end

irb(main):021:0> array_chomp a
=> ["one", 3.14, 5]

On Sat, Apr 2, 2016 at 7:03 PM, A Berger <aberger7890@gmail.com> wrote:
> Hello
> How could I efficiently do Array.chomp for removing only nils at the
end?
>
> (This would be a nice addifional feature!)
> Thank you
> Berg
>
>
>
> Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;
>

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi

I propose this to be added to ruby (core):

class Array
def chomp
  a.pop while a.last.nil? && a.size>0
  return a
end
end

Is this correct (in all circumstances)?
Is this efficient (I assume that rindex iterates each time from the
beginning x[0] ) ?
- Is there (internally) a (direct) link to the last element (for pop)?
If not, using .reverse could be even more efficient (but more
mem-consuming)?

Thank you
Berg

What's the real world use case? I personally cannot remember
a single time I had to do this with an array; usually you would
either generate arrays that don't contain nil in the first place,
or, if nil values can occur, use Array#compact to get rid of all
of them.

Why would you want to keep part of the nil values?

(And, to be efficient it would have to be implemented in C.)

Regards,
Marcus

···

Am 03.04.2016 um 13:03 schrieb A Berger:

I propose this to be added to ruby (core):

class Array
def chomp
  a.pop while a.last.nil? && a.size>0
  return a
end
end

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

I propose this to be added to ruby (core):

What would be the general use case that warrants addition to the core class?

class Array
def chomp
  a.pop while a.last.nil? && a.size>0
  return a
end
end

Is this correct (in all circumstances)?

No.

Is this efficient (I assume that rindex iterates each time from the
beginning x[0] ) ?

Measure different approaches and you'll know. Module Benchmark will help here.

- Is there (internally) a (direct) link to the last element (for pop)?

What do you mean?

If not, using .reverse could be even more efficient (but more
mem-consuming)?

?

robert

···

On Sun, Apr 3, 2016 at 1:03 PM, A Berger <aberger7890@gmail.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/