Repository: JEG2/highline (Ruby)

Issue:
default value has to be a string
I'm not sure this is intentional.

The default value for ask has to be a string, if it is an integer, it is silently ignored. 

Tested with v1.7.10

This was not the case prior to v1.7.0

Demo:

```
require 'highline'                                                                                        
                                                                                                          
cli = HighLine.new                                                                                        
                                                                                                          
cli.ask("Default string works", Integer) { |q| q.default = "123" }                                        
cli.ask("Default integer doesn't", Integer) { |q| q.default = 456 }
```


Discussion: Hi @branch14 ,

I don't plan to patch 1.7-stable branch anymore.
I couldn't reproduce the issue at 2.0 branch.

@branch14 today we have officially released 2.0.0 version!
I'll close this issue, but feel free to reopen it if you think it's necessary.

Thank you very much for your feedback! 👍 
Thank you for your work on highline. I never tried the 2.0 branch, but now that it has been release I totally will. :)
Thank you! I hope it work well. Give me a feedback as soon as you tested it 👍 
I tested it and unfortunately I found the same issue in 2.0.0

Luckily the work around still works. :)

Here is how to reproduce it:

```
#!/usr/bin/env ruby

require 'highline'
h = HighLine.new

h.ask("Default shows if it is a string: ", String) { |q| q.default = "I am a string" }
h.ask("Default doesn't show if it is an int: ", Integer) { |q| q.default = 42 }
h.ask("But strangely this works: ", Integer) { |q| q.default = "42" }

# % ruby test.rb
# Default shows if it is a string: |I am a string|
# Default doesn't show if it is an int:
# But strangely this works: |42|
I've reopened the issue.
Could you clone the repo and run  ```rake acceptance``` and post the results here?
It was correctly reproduced in my enviroment.
In a quick look what I see is...

Default value is about the **answer gathering** part. We should always receive a string by keyboard, although we could have any kind of object as default. This could be useful for several scenarios by passing as default a more meaningful object than `nil`.

That `Integer` is more about **coercion**.

I can see that Highline is doing a type check at this part

```ruby
    def build_responses(message_source = answer_type)
      append_default if [::String, Symbol].include? default.class

      new_hash = build_responses_new_hash(message_source)
      # Update our internal responses with the new hash
      # generated from the message source
      @internal_responses = @internal_responses.merge(new_hash)
    end
```

I would prefer to just cast it into a String and check if it is empty or not (to avoid placing two bars side by side without any content inside it. Like ||).

But, any object in Ruby has a string representation by default. And, at that case that I pass bare object I could have the weird result like this:
```"Question:  |#<Object:0xXXXXXX>|  "```

From: TestHighLine#test_default_with_non_String_objects [/Users/abinoam/src/highline/test/test_highline.rb:834

So, perhaps sticking with the type checking thing, although not being the more flexible way, solves your problem without any great change at the behaviour of HighLine.
I understand that inputs are always of type string and that the Integer is used for coercion.

I concur that `"Question: |#<Object:0xXXXXXX>| "` seems weird. But at least it would follow PLOP. Whereas silently ignoring everything except Strings and Symbols violates PLOP.

Personally I would prefer "Garbage in, Garbage out", as it would have been obvuious that I need to pass something with a meaningful string representation.

With the current solution it took me a while to figure out that I had to cast the Integer to a String to make it work.
I may agree with you. But this breaks the current behaviour. We can discuss this to come in a major release.

For now, I think we can ammend the type checking part adding Integer to the list.
This will solve for your specific case without any major break.
What do you think about this?
Hi @branch14 what do you think about my last comment 👆 
I fully concur. I'm sorry I didn't mean to let you hanging.

On Mon, 3 Sep 2018, 17:08 Abinoam P. Marques Jr. <notifications@github.com>
wrote:

> Hi @branch14 <https://github.com/branch14> what do you think about my
> last comment 👆
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/JEG2/highline/issues/233#issuecomment-418141169>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AABR5H6jK9-3A9jKTTVqK_ymEr4rBl5uks5uXUXqgaJpZM4Ud3vp>
> .
>

Hi @branch14 

Sorry for taking so long.
I'll add the requested behavior at the same time adding a switch do disable it.
So people can easily transition between the to behaviors (showing or not showing the Question#default value hint).
Code is ready, opening a PR.
I'll have to do a major release as this is a breaking change.



Fix (patch):
diff --git a/Changelog.md b/Changelog.md
index fb665c2..9a86f8a 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,6 +2,10 @@
 
 Below is a complete listing of changes for each revision of HighLine.
 
+### Unreleased 2.2.0.develop.1
+* I #233 Show Question#default hint for non String values (@abinoam)
+  * Add Question#default_hint_show to allow disabling it.
+
 ### 2.1.0 / 2022-12-31
 * PR #255 - Change minimum Ruby version requirement to 2.3 (@abinoam)
 * PR #254 - Improve Github Actions file (@abinoam)
diff --git a/README.md b/README.md
index 740b446..c1baf49 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,15 @@ puts "You have answered: #{answer}"
 
 cli.ask("Company?  ") { |q| q.default = "none" }
 
+## Disable default value hint showing
+
+my_special_default_object = Object.new
+
+cli.ask("Question?  ") do |q|
+  q.default = my_special_default_object
+  q.default_hint_show = false
+end
+
 
 # Validation
 
diff --git a/lib/highline/question.rb b/lib/highline/question.rb
index 50478ed..ce2245d 100644
--- a/lib/highline/question.rb
+++ b/lib/highline/question.rb
@@ -56,6 +56,7 @@ def initialize(template, answer_type)
       @completion  = @answer_type
 
       @echo         = true
+      @default_hint_show = true
       @whitespace   = :strip
       @case         = nil
       @in           = nil
@@ -136,6 +137,10 @@ def initialize(template, answer_type)
     attr_accessor :case
     # Used to provide a default answer to this question.
     attr_accessor :default
+    # Set it to a truthy or falsy value to enable or disable showing the default
+    # value hint between vertical bars (pipes) when asking the question.
+    # Defaults to +true+
+    attr_accessor :default_hint_show
     #
     # If set to a Regexp, the answer must match (before type conversion).
     # Can also be set to a Proc which will be called with the provided
@@ -252,7 +257,7 @@ def answer_or_default(answer_string)
     #   Same as {#answer_type}.
 
     def build_responses(message_source = answer_type)
-      append_default if [::String, Symbol].include? default.class
+      append_default_to_template if default_hint_show
 
       new_hash = build_responses_new_hash(message_source)
       # Update our internal responses with the new hash
@@ -607,15 +612,20 @@ def get_echo_for_response(response)
     # Trailing whitespace is preserved so the function of HighLine.say() is
     # not affected.
     #
-    def append_default
+    def append_default_to_template
+      return unless default.respond_to? :to_s
+
+      default_str = default.to_s
+      return if default_str.empty?
+
       if template =~ /([\t ]+)\Z/
-        template << "|#{default}|#{Regexp.last_match(1)}"
+        template << "|#{default_str}|#{Regexp.last_match(1)}"
       elsif template == ""
-        template << "|#{default}|  "
+        template << "|#{default_str}|  "
       elsif template[-1, 1] == "\n"
-        template[-2, 0] = "  |#{default}|"
+        template[-2, 0] = "  |#{default_str}|"
       else
-        template << "  |#{default}|"
+        template << "  |#{default_str}|"
       end
     end