0

This is my code:

my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages]

# Add your code below!
symbol_filter = lambda {|x| if(x.is_a? Symbol) puts x end}

enter image description here

I want to create a lambda which will filter for symbols in the array.

3
  • A screenshot is usually not necessary. Commented Sep 17, 2014 at 19:26
  • It's not necessary to tell us you're new or ask for forgiveness. If you've done a reasonably thorough search and ask the question well you'll have no problems. Commented Sep 17, 2014 at 19:28
  • @theTinMan - despit that, sh!t still happens. So, I play safe Commented Sep 17, 2014 at 19:43

2 Answers 2

1

The error is as follows:

syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'

And it is pointing at puts, which means puts is what was unexpected.

So the solutions are to give it what it expects

if ( x.isA? Symbol ) then puts x end

if ( x.isA? Symbol ); puts x end

if ( x.isA? Symbol )
  puts x
end

As a side, there is another syntax for one-liners like this

puts x if ( x.isA? Symbol )
Sign up to request clarification or add additional context in comments.

Comments

1

When using if conditions in one single line, you have to end your if header with a ';'. However I would highly recommend against those ifs. Moreover you should write something like this:

if x.is_a?(Symbol)
  p x
end

or use the proper inline syntax:

p x if x.is_a?(Symbol)

Back to your problem, what do you want to achieve? Find all symbols in the array? Then you should use select on that array

Comments

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.