[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

iteration the ruby way

Navindra Umanee

2/4/2005 10:06:00 PM

Hi,

I have a question about The Ruby Way. Pickaxe gives the following
example for using iterators in Ruby:

a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }

This outputs:

a -- b -- c --

But what if I want to print "a -- b -- c"? What's the proper Ruby way
of doing that? Is there some way of detecting the last element? I
mean, obviously I can do it with a for/while loop or something...

Thanks,
Navin.



17 Answers

John Wilger

2/4/2005 10:14:00 PM

0

On Sat, 5 Feb 2005 07:05:51 +0900, Navindra Umanee
<navindra@cs.mcgill.ca> wrote:
> Hi,
>
> I have a question about The Ruby Way. Pickaxe gives the following
> example for using iterators in Ruby:
>
> a = [ "a", "b", "c" ]
> a.each {|x| print x, " -- " }
>
> This outputs:
>
> a -- b -- c --
>
> But what if I want to print "a -- b -- c"? What's the proper Ruby way
> of doing that?

a = ["a","b","c"]
puts a.join(' - ')

--
Regards,
John Wilger

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland


Francis Hwang

2/4/2005 10:14:00 PM

0

Use join:

a = [ "a", "b", "c" ]
puts a.join( " -- " )

And while we're at it, you can use the %w( ... ) construct to quickly
make an array of strings:

puts %w( a b c ).join( " -- " )

Joe Van Dyk

2/4/2005 10:15:00 PM

0

On Sat, 5 Feb 2005 07:05:51 +0900, Navindra Umanee
<navindra@cs.mcgill.ca> wrote:
> Hi,
>
> I have a question about The Ruby Way. Pickaxe gives the following
> example for using iterators in Ruby:
>
> a = [ "a", "b", "c" ]
> a.each {|x| print x, " -- " }
>
> This outputs:
>
> a -- b -- c --
>
> But what if I want to print "a -- b -- c"? What's the proper Ruby way
> of doing that? Is there some way of detecting the last element? I
> mean, obviously I can do it with a for/while loop or something...
>
> Thanks,
> Navin.
>

This probably isn't what you're looking for... but:

puts a.join(" -- ")


Navindra Umanee

2/4/2005 10:19:00 PM

0

John Wilger <johnwilger@gmail.com> wrote:
> > I have a question about The Ruby Way. Pickaxe gives the following
> > example for using iterators in Ruby:
> >
> > a = [ "a", "b", "c" ]
> > a.each {|x| print x, " -- " }
> >
> > This outputs:
> >
> > a -- b -- c --
> >
> > But what if I want to print "a -- b -- c"? What's the proper Ruby way
> > of doing that?
>
> a = ["a","b","c"]
> puts a.join(' - ')

I guess I over-simplified. I really want to do computations based on
each element and compute my output. However, it's slightly different
for the last element. I would like to detect the last element in the
iteration.

For example:

bottom_items.each{ |item|
do_something_with(item[0], item[1], item[2])
if_not_last_item_do_this
}

Thanks,
Navin.


dblack

2/4/2005 10:24:00 PM

0

Francis Hwang

2/4/2005 10:28:00 PM

0

I'd probably use Enumerable#each_with_index.

ary = [ 1, 2, 3, 4 ]
ary.each_with_index { |elt, i|
if i < ary.size - 1
puts elt
else
puts elt * 10
end
}

which will output:

1
2
3
40

Navindra Umanee

2/4/2005 10:33:00 PM

0


Thanks guys!

Cheers,
Navin.


Martin DeMello

2/4/2005 10:49:00 PM

0

David A. Black <dblack@wobblini.net> wrote:
> If your enumerable object has a size method, you can do:
>
> bottom_items.each_with_index do |item,i|
> do_something_with(...)
> unless i == bottom_items.size - 1
> ...
> end
> end
>
> or something similar with #each_index.

And if it doesn't, this approach should work:

irb(main):005:0> b = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):006:0> b.inject(lambda {}) {|a,v| a.call; puts v; a = lambda { puts "-
--" }}

martin

p.s. who'd have thunk it

Eric Hodel

2/4/2005 11:06:00 PM

0

On 04 Feb 2005, at 14:18, Navindra Umanee wrote:

> John Wilger <johnwilger@gmail.com> wrote:
>>> I have a question about The Ruby Way. Pickaxe gives the following
>>> example for using iterators in Ruby:
>>>
>>> a = [ "a", "b", "c" ]
>>> a.each {|x| print x, " -- " }
>>>
>>> This outputs:
>>>
>>> a -- b -- c --
>>>
>>> But what if I want to print "a -- b -- c"? What's the proper Ruby
>>> way
>>> of doing that?
>>
>> a = ["a","b","c"]
>> puts a.join(' - ')
>
> I guess I over-simplified. I really want to do computations based on
> each element and compute my output. However, it's slightly different
> for the last element. I would like to detect the last element in the
> iteration.
>
> For example:
>
> bottom_items.each{ |item|
> do_something_with(item[0], item[1], item[2])
> if_not_last_item_do_this
> }

items = items.map do |item|
do_something_with item
end

puts items.join(' -- ')


--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Robert Klemme

2/4/2005 11:29:00 PM

0


"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:jZSMd.271421$Xk.130554@pd7tw3no...
> David A. Black <dblack@wobblini.net> wrote:
>> If your enumerable object has a size method, you can do:
>>
>> bottom_items.each_with_index do |item,i|
>> do_something_with(...)
>> unless i == bottom_items.size - 1
>> ...
>> end
>> end
>>
>> or something similar with #each_index.
>
> And if it doesn't, this approach should work:
>
> irb(main):005:0> b = [1,2,3,4,5]
> => [1, 2, 3, 4, 5]
> irb(main):006:0> b.inject(lambda {}) {|a,v| a.call; puts v; a = lambda {
> puts "-
> --" }}
>
> martin
>
> p.s. who'd have thunk it

Interesting idea to use a proc. I never thought of that. Btw, you don't
need the assignment to "a".

>> b.inject(lambda {}) {|a,v| a.call; print v; lambda {print " -- "}}
1 -- 2 -- 3 -- 4 -- 5=> #<Proc:0x100c43a0@(irb):12>


Um... Here's another one - without lambdas:

>> b = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
>> print b.inject{|x,y| print x, " -- ";y}, "\n"
1 -- 2 -- 3 -- 4 -- 5
=> nil
>> puts b.inject{|x,y| print x, " -- ";y}
1 -- 2 -- 3 -- 4 -- 5
=> nil

If you really need two different manipulation functions for the first n-1
elements and the last element you can do:

puts transform_last( b.inject{|x,y| print transform_first_n(x), " -- ";y} )

There's a nice speciality about inject with no arguments: on the first call
the two block args are set to the first and second iteration elements...

:-)

Kind regards

robert