Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Version issues

Reply
Thread Tools

Version issues

 
 
David Vlad
Guest
Posts: n/a
 
      03-19-2010
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/.

 
Reply With Quote
 
 
 
 
Luis Lavena
Guest
Posts: n/a
 
      03-19-2010
On Mar 19, 11:11*pm, David Vlad <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
brabuhr@gmail.com
Guest
Posts: n/a
 
      03-19-2010
On Fri, Mar 19, 2010 at 6:11 PM, David Vlad <(E-Mail Removed)> 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 )

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      03-20-2010

On Mar 19, 2010, at 3:11 PM, David Vlad <(E-Mail Removed)>
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.

 
Reply With Quote
 
David Vlad
Guest
Posts: n/a
 
      03-20-2010
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/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      03-20-2010

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"]



 
Reply With Quote
 
David Vlad
Guest
Posts: n/a
 
      03-20-2010
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/.

 
Reply With Quote
 
William Rutiser
Guest
Posts: n/a
 
      03-20-2010
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




 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      03-20-2010
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.

 
Reply With Quote
 
David Vlad
Guest
Posts: n/a
 
      03-20-2010
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[i] = array[i] * 2
i += 1
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
Re: Windows XP Pro clean install issues, SP2 issues too... pcbutts1 Computer Support 0 07-06-2005 04:52 PM
SNMP Issues in Cisco Routers; Vulnerability Issues in TCP =?iso-8859-1?Q?Frisbee=AE?= MCSE 0 04-21-2004 03:00 PM



Advertisments