Reputation: 22704
Lets suppose we have an array
p = String.new
a = ['d','e','f','g','h']
a.each do |value|
p << value
p << "%" if value is not last
end
I need to determine whether the a['h'] is the last index value or not?
Is there a shortcut way to find it like a.is_last?()
Upvotes: 1
Views: 193
Reputation: 4520
Using your specific example, you can just use Array.join. You can then replace your example with:
a = ["d", "e", "f", "g", "h"]
p = a.join("%") # d%e%f%g%h
Upvotes: 8
Reputation: 22704
I have done this in the way
p = String.new
a = ['d','e','f','g','h']
a.each do |value|
p << value
p << "%" if a.last != value
end
I need other then this?
Upvotes: 2