Version issues

Discussion in 'Ruby' started by David Vlad, Mar 19, 2010.

  1. David Vlad

    David Vlad Guest

    I am using ruby 1.8.6 and I have been having some problem using certain
    methods and stuff. It seems that my ruby wont recognize until loops and
    also the .collect method dosnt work.

    Is this normal? If I try to write use a until loop in my code I get
    error and if I try to use .collect nothing happens (but no error).
    --
    Posted via http://www.ruby-forum.com/.
    David Vlad, Mar 19, 2010
    #1
    1. Advertising

  2. David Vlad

    Luis Lavena Guest

    On Mar 19, 11:11 pm, David Vlad <> wrote:
    > I am using ruby 1.8.6 and I have been having some problem using certain
    > methods and stuff. It seems that my ruby wont recognize until loops and
    > also the .collect method dosnt work.
    >
    > Is this normal? If I try to write use a until loop in my code I get
    > error and if I try to use .collect nothing happens (but no error).


    Please show a code example, the output you're getting and the exact
    Ruby version you're using.

    Help us help you
    --
    Luis Lavena
    Luis Lavena, Mar 19, 2010
    #2
    1. Advertising

  3. David Vlad

    Guest

    On Fri, Mar 19, 2010 at 6:11 PM, David Vlad <> wrote:
    > I am using ruby 1.8.6 and I have been having some problem using certain
    > methods and stuff. It seems that my ruby wont recognize until loops and
    > also the .collect method dosnt work.
    >
    > Is this normal? If I try to write use a until loop in my code I get
    > error and if I try to use .collect nothing happens (but no error).


    Please provide a short example of the code you are trying and the
    incorrect output you are receiving.

    Thanks.

    PS
    ( http://catb.org/~esr/faqs/smart-questions.html )
    , Mar 19, 2010
    #3
  4. David Vlad

    Ryan Davis Guest

    On Mar 19, 2010, at 3:11 PM, David Vlad <>
    wrote:

    > I am using ruby 1.8.6 and I have been having some problem using
    > certain
    > methods and stuff. It seems that my ruby wont recognize until loops
    > and
    > also the .collect method dosnt work.
    >
    > Is this normal? If I try to write use a until loop in my code I get
    > error and if I try to use .collect nothing happens (but no error).


    Is what normal? We can't help you if we can't see what you're trying
    to do. "doesn't work" is not descriptive.
    Ryan Davis, Mar 20, 2010
    #4
  5. David Vlad

    David Vlad Guest

    I apologize. I actually managed to get the until loops working after
    some reinstalling. However, the .collect method still dosnt work and I
    really want to use it. The code looks like this:


    array = [1,2,5,7]

    array.collect do |x|
    x = x*2
    end

    puts array.inspect


    After I have run this code it still says the array is [1,2,5,7].

    --
    Posted via http://www.ruby-forum.com/.
    David Vlad, Mar 20, 2010
    #5
  6. David Vlad

    Ryan Davis Guest

    On Mar 20, 2010, at 03:51 , David Vlad wrote:

    > I apologize. I actually managed to get the until loops working after
    > some reinstalling. However, the .collect method still dosnt work and I
    > really want to use it. The code looks like this:
    >
    >
    > array = [1,2,5,7]
    >
    > array.collect do |x|
    > x = x*2
    > end
    >
    > puts array.inspect
    >
    >
    > After I have run this code it still says the array is [1,2,5,7].


    % ri Array.collect
    ---------------------------------------------------------- Array#collect
    array.collect {|item| block } -> an_array
    array.map {|item| block } -> an_array
    ------------------------------------------------------------------------
    Invokes _block_ once for each element of _self_. Creates a new
    array containing the values returned by the block. See also
    +Enumerable#collect+.

    a = [ "a", "b", "c", "d" ]
    a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
    a #=> ["a", "b", "c", "d"]
    Ryan Davis, Mar 20, 2010
    #6
  7. David Vlad

    David Vlad Guest

    Ryan Davis wrote:
    > ---------------------------------------------------------- Array#collect
    > array.collect {|item| block } -> an_array
    > array.map {|item| block } -> an_array
    > ------------------------------------------------------------------------
    > Invokes _block_ once for each element of _self_. Creates a new
    > array containing the values returned by the block. See also
    > +Enumerable#collect+.
    >
    > a = [ "a", "b", "c", "d" ]
    > a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
    > a #=> ["a", "b", "c", "d"]


    I know what it does, but still it dosnt work.
    --
    Posted via http://www.ruby-forum.com/.
    David Vlad, Mar 20, 2010
    #7
  8. David Vlad wrote:
    > I apologize. I actually managed to get the until loops working after
    > some reinstalling. However, the .collect method still dosnt work and I
    > really want to use it. The code looks like this:
    >
    >
    > array = [1,2,5,7]
    >
    > array.collect do |x|
    > x = x*2
    > end
    >
    > puts array.inspect
    >
    >
    > After I have run this code it still says the array is [1,2,5,7].


    "array.collect " isn't supposed to modify array.

    "array.collect" returns a newly constructed array containing the block's
    results.

    Compare

    irb(main):001:0> array = [1,2,5,7]
    => [1, 2, 5, 7]
    irb(main):002:0>
    irb(main):003:0* array.collect do |x|
    irb(main):004:1* x = x*2
    irb(main):005:1> end
    => [2, 4, 10, 14]
    irb(main):006:0>
    irb(main):007:0* puts array.inspect
    [1, 2, 5, 7]
    => nil

    with

    irb(main):008:0> array = [1,2,5,7]
    => [1, 2, 5, 7]
    irb(main):009:0> puts array.collect do |x|
    irb(main):010:1* x = x*2
    irb(main):011:1> end
    1
    2
    5
    7
    => nil
    irb(main):012:0> puts array.inspect
    [1, 2, 5, 7]
    => nil
    William Rutiser, Mar 20, 2010
    #8
  9. On 20.03.2010 13:25, David Vlad wrote:

    > I know what it does, but still it dosnt work.



    The behavior of the code you posted is entirely consistent with
    the described behavior and the example in the docs which Ryan posted
    (in the example the contents of a are the same before and after calling
    collect on it). So if collect does not work for you, show an example in
    which it doesn't work (as opposed to one where it behaves exactly as
    expected).

    To summarize the important part of the docs: collect returns a *new* array,
    it does not change the array it is called on in any way.
    Sebastian Hungerecker, Mar 20, 2010
    #9
  10. David Vlad

    David Vlad Guest

    Im so sorry, I have misunderstood everything. I thought it was supposed
    to modify the array not create a new one within the block.

    Is there any method that actually does modify the array or will I have
    to do something like this:

    array = [1,3,7,8]
    i = 0

    while i < array.length
    array = array * 2
    i += 1
    end
    --
    Posted via http://www.ruby-forum.com/.
    David Vlad, Mar 20, 2010
    #10
  11. On 20.03.2010 17:20, GianFranco Bozzetti wrote:
    > array = [1,3,7,8]
    > array.collect! {|x| x*2 } # => [2,6,14,16]
    >

    And most importantly:
    array # => [2,6,14,16]
    Sebastian Hungerecker, Mar 20, 2010
    #11
  12. David Vlad

    Ryan Davis Guest

    On Mar 20, 2010, at 05:25 , David Vlad wrote:

    > Ryan Davis wrote:
    >> ---------------------------------------------------------- Array#collect
    >> array.collect {|item| block } -> an_array
    >> array.map {|item| block } -> an_array
    >> ------------------------------------------------------------------------
    >> Invokes _block_ once for each element of _self_. Creates a new
    >> array containing the values returned by the block. See also
    >> +Enumerable#collect+.
    >>
    >> a = [ "a", "b", "c", "d" ]
    >> a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
    >> a #=> ["a", "b", "c", "d"]

    >
    > I know what it does, but still it dosnt work.


    You seem to be missing the big clue: "CREATES A NEW ARRAY"

    P.S. "it doesn't work" is NOT descriptive.
    Ryan Davis, Mar 20, 2010
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad Crowder

    Framwork Version issues

    Chad Crowder, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    331
    Natty Gur
    Aug 13, 2003
  2. chaser7016
    Replies:
    3
    Views:
    364
    chaser7016
    Jan 3, 2007
  3. V Green
    Replies:
    0
    Views:
    808
    V Green
    Feb 5, 2008
  4. PA Bear [MS MVP]
    Replies:
    0
    Views:
    908
    PA Bear [MS MVP]
    Feb 5, 2008
  5. MowGreen [MVP]
    Replies:
    5
    Views:
    1,983
    PA Bear [MS MVP]
    Feb 9, 2008
Loading...

Share This Page