Ruby - Is it possible to use a ternary operator in a proc?

Cities

I'm going through TestFirst.org's Ruby course, and I ran into a coding challenge that required me to construct a method "titleize" that capitalizes every word in a string except 'little words'.

The following code solves the problem.

def titleize(words)
  little_words = %w[a an the and but or for nor on at to from by over]

  capitalizer = Proc.new do |word|
    if little_words.include? word
      word
    else
      word.capitalize
    end
  end

  words.split.map(&capitalizer)*' '
end

I originally tried using the following proc.

capitalizer = Proc.new { |word| little_words.include? word ? word : word.capitalize }

However, this proc only returned truth values. The truth values came from whether or not the word being tested was included in the little_words array. This leads me to believe that the proc is returning before it even evaluates the code to the right of the question mark.

I read the Ruby documentation for both procs and the ternary operator, but I could find nothing regarding whether or not the two can be used together.

I searched Stack Overflow for "proc ruby ternary" and read through the similar questions that popped up as I began to post this question. I couldn't find anything there either.

And so, I come to you, the geniuses of Stack Overflow. I have a question.

In Ruby, can one use a ternary operator in a proc?

(and if so, why didn't my little proc work?)

seph

Try:

capitalizer = Proc.new { |word| little_words.include?(word) ? word : word.capitalize }

That should work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ruby ternary operator (or) or operator

From Dev

Is it possible to use the ternary operator "?" to fill an array list in C/C++?

From Dev

Is it possible to use the ternary operator in lazy instantiation of a getter in Objective C?

From Dev

Is it recommended to use Ternary operator?

From Dev

use of ternary operator in javascript

From Dev

Ruby ternary operator not returning as expected

From Dev

Why is it not possible to overload the ternary operator?

From Dev

c++ ternary operator use

From Dev

How to use Ternary Operator in JSP

From Dev

Using Ruby's ternary operator ? : to shorten this

From Dev

Ruby ternary operator in erb with html tags

From Dev

2-Level Ruby Ternary Operator Not Working

From Dev

Using Ruby's ternary operator ? : to shorten this

From Dev

Is it possible to put the ternary operator inside a function call?

From Dev

NullPointerException throws when I use ternary operator

From Dev

Can ngClass use ternary operator in Angular 2?

From Java

How to use the ternary operator inside an interpolated string?

From Dev

How to use ternary operator in SQL Server 2008?

From Dev

AWK|BASH, use double FS and ternary operator

From Dev

How to use continue keyword in a ternary operator in php

From Dev

How to use AND in if statement using ternary operator

From Dev

Is it ok to use ternary operator in C++ streams?

From Dev

How to use php ternary operator in the parameter of a function?

From Dev

How to use continue keyword in a ternary operator in php

From Dev

How to use AND in if statement using ternary operator

From Dev

Use ternary operator for flow control in Java

From Dev

Is it ok to use ternary operator in C++ streams?

From Dev

Unable to use ternary operator within object data

From Dev

use ternary operator to solve multiple conditions

Related Related

  1. 1

    Ruby ternary operator (or) or operator

  2. 2

    Is it possible to use the ternary operator "?" to fill an array list in C/C++?

  3. 3

    Is it possible to use the ternary operator in lazy instantiation of a getter in Objective C?

  4. 4

    Is it recommended to use Ternary operator?

  5. 5

    use of ternary operator in javascript

  6. 6

    Ruby ternary operator not returning as expected

  7. 7

    Why is it not possible to overload the ternary operator?

  8. 8

    c++ ternary operator use

  9. 9

    How to use Ternary Operator in JSP

  10. 10

    Using Ruby's ternary operator ? : to shorten this

  11. 11

    Ruby ternary operator in erb with html tags

  12. 12

    2-Level Ruby Ternary Operator Not Working

  13. 13

    Using Ruby's ternary operator ? : to shorten this

  14. 14

    Is it possible to put the ternary operator inside a function call?

  15. 15

    NullPointerException throws when I use ternary operator

  16. 16

    Can ngClass use ternary operator in Angular 2?

  17. 17

    How to use the ternary operator inside an interpolated string?

  18. 18

    How to use ternary operator in SQL Server 2008?

  19. 19

    AWK|BASH, use double FS and ternary operator

  20. 20

    How to use continue keyword in a ternary operator in php

  21. 21

    How to use AND in if statement using ternary operator

  22. 22

    Is it ok to use ternary operator in C++ streams?

  23. 23

    How to use php ternary operator in the parameter of a function?

  24. 24

    How to use continue keyword in a ternary operator in php

  25. 25

    How to use AND in if statement using ternary operator

  26. 26

    Use ternary operator for flow control in Java

  27. 27

    Is it ok to use ternary operator in C++ streams?

  28. 28

    Unable to use ternary operator within object data

  29. 29

    use ternary operator to solve multiple conditions

HotTag

Archive