0

I'm trying to loop through and match the following string:

@custom_channels = { cnn: 2, abc: 90 }

I then want to be able to check/match against each and return the channel number (e.g. 90):

listen_for /channel to (#{@custom_channels.join('|')})/i do |name|
    change_channel @custom_channels[name.downcase]
end

I keep getting the following error:

scraper.rb:5:in `<main>': undefined method `join' for {:cnn=>2, :abc=>90}:Hash (NoMethodError)
1
  • Why do you use join here? What are you trying to achieve with this? Commented Dec 23, 2011 at 4:41

2 Answers 2

1

If I understand what you are asking for:

@custom_channels = { cnn: 2, abc: 90 }

channel_names = @custom_channels.keys.map(&:to_s)

listen_for /channel to (#{Regexp.union(channel_names)})/i do |name|
    change_channel @custom_channels[name.to_sym]
end
Sign up to request clarification or add additional context in comments.

3 Comments

Do you have a typo? {cnn: 2, abc: 90}.keys #=> [:cnn, :abc]
Do you have another typo? Regexp.union([:cnn,:abc].map(&:to_s)) #=> /cnn|abc/
I fixed this by adding: #{Regexp.union({ cnn: 2, abc: 90 }} it didn't like @custom_channels.keys in there.
1

Try this one:

1.9.3p0 :007 >   channels = {:cnn=>2, :abc=>90}
 => {:cnn=>2, :abc=>90} 
1.9.3p0 :008 > channels.keys
 => [:cnn, :abc] 
1.9.3p0 :009 > channels.keys.each do |name|
1.9.3p0 :010 >     puts channels[name]
1.9.3p0 :011?>   end
2
90
 => [:cnn, :abc] 

5 Comments

I need it to return the actual number not the text.
What is the actual number you expect?
If I ask for "cnn" I want to return: 2
Are you not able to modify it yourself?
@custom_channels[:cnn] #=> 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.