I'd like the cycle method to take an array of values which I compile on the fly, but it seems to be interpreting it not as I'd have hoped.
(In this example it's a static array, but I want it to work so that I can use arrays that are constructed variably)
- some_array = ['one', 'two', 'three']
- colors.each do |color|
%a{ :name => color, :class => "#{cycle(some_array)}" }
That applies this as a class to each element:
"three"] "two", ["one",
开发者_StackOverflow社区
...looks as though it's calling to_s
on the array or something.
How am I supposed to be doing this?
cycle
takes multiple arguments and cycles through them. You're passing a single argument, an array.
You can use the splat operator to change the array into these multi arguments:
cycle(*some_array)
This will act as if you did:
cycle("one", "two", "three")
Rather than:
cycle(["one", "two", "three"])
精彩评论