开发者

How do I pass Rails' cycle() an array of values to cycle through?

开发者 https://www.devze.com 2023-01-18 07:50 出处:网络
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.

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

精彩评论

暂无评论...
验证码 换一张
取 消