What will give me something like ruby readline with a default value? - ruby

If I want to have a prompt on the terminal with a default value already typed in, how can I do that?
Ruby's standard Readline.readline() lets me set the history but not fill in a default value (as far as I can tell, at least)
I would like something like this:
code:
input = Readline.readline_with_default('>', 'default_text')
console:
> default_text|

What you are asking is possible with Readline. There's a callback where you can get control after the prompt is displayed and insert some text into the read buffer.
This worked for me:
Readline.pre_input_hook = -> do
Readline.insert_text "hello.txt"
Readline.redisplay
# Remove the hook right away.
Readline.pre_input_hook = nil
end
input = Readline.readline("Filename: ", false)
puts "-- input:#{input.inspect}"
BTW, I fairly tried to use HighLine, but it appeared to be a no-alternative to me. One of the disappointing reasons was the fact that HighLine#ask reads cursor movement keys as regular input. I stopped looking in that direction after that sort of discovery.

+1 to highline
try with something like:
require 'highline/import'
input = ask('> ') {|q| q.default = 'default_text'} # > |default_text|

Sounds like a job for ncurses. Seems like rbcurse (http://rbcurse.rubyforge.org/) is the best maintained API at the moment.

I'm struggling with the same thing.
The way I'm doing it right now is:
options = ["the_text_you_want"]
question = "use TAB or up arrow to show the text > "
Readline.completion_append_character = " "
Readline::HISTORY.push options.first
Readline.completion_proc = proc { |s| options.grep( /^#{Regexp.escape(s)}/ ) }
while value = Readline.readline(question, true)
exit if value == 'q'
puts value.chomp.strip #do something with the value here
end
yes, it's silly, but it has been the only way I've found to do it.
did anybody find any solution to this?

Highline doesn't do exactly what you describe, but maybe it's close enough.

New answer to an old question, but adding because I found this looking for an answer to the same question.
tty-prompt looks like it does what you are looking for - asking for input with an editable default. It's the only gem I found that would give me the editable default (but there may be others)
Full code to to that would look like:
require "tty-prompt"
prompt = TTY::Prompt.new
input = prompt.ask('What is your name?', value: 'Bob')

Related

How to use polymorphism to remove a switch statement which compares strings?

I am new to Ruby, so let me describe the context of my problem first:
I have a json as input which has the following key / value pair:
{
"service": "update"
}
The value has many different values for example: insert,delete etc.
Next there is a method x which handles the different requests:
def x(input)
case input[:service]
services = GenericService.new
when "update"
result = services.service(UpdateService.new,input)
when "insert"
result = services.service(InsertService.new,input)
when "delete"
result = services.service(DeleteService.new,input)
....
....
else
raise "Unknown service"
end
puts JSON.pretty_generate(result)
end
What is bothering me is that I still need to use a switch statement to check the String values (reminds me of 'instance of' ugh..). Is there a cleaner way (not need to use a switch)?
Finally I tried to search for an answer to my question and did not succeed, if however I missed it feel free to comment the related question.
Update: I was thinking to maybe cast the string to the related class name as follows: How do I create a class instance from a string name in ruby? and then call result = services.services(x.constantize.new,input) , then the class names ofcourse needs to match the input of the json.
You can try something like:
def x(input)
service_class_name = "#{input[:service].capitalize}Service"
service_class = Kernel.const_get(service_class_name)
service_class.new(input).process
end
In addition you might want to check if this is a valid Service class name at all.
I don't understand why you want to pass the service to GenericService this seems strange. let the service do it's job.
If you're trying to instatiate a class by it's name you're actually speaking about Reflection rather than Polymorphism.
In Ruby you can achieve this in this way:
byName = Object.const_get('YourClassName')
or if you are in a Rails app
byName= 'YourClassName'.constantize
Hope this helps
Just first thoughts, but you can do:
eval(services.service("#{input[:service].capitalize}Service.new, #{input})") if valid_service? input[:service]
def valid_service?
w%(delete update insert).include? input[:service]
end
As folks will no doubt shout, eval needs to be used with alot of care

passing value (through variable) to custom function

I'm trying this thing for a while but can't figure out what am I doing wrong.
Here is sample function (which is similar to the original one, except for the hash, which is generated dynamically in the original one):
module Puppet::Parser::Functions
newfunction(:am_running_oss, :type => :rvalue ) do |args|
oss = {:linux=>["Slackware", "RedHat", "Caldera"],
:mac=>["Jaguar", "Lion", "Tiger", "Kodiak"],
:win=>["Chicago", "Daytona", "Longhorn"]}
cls = args[0]
if oss.key?(cls)
return oss[cls][0]
else
return 'undefined'
end
end
end
and then in my manifest, I have this:
$h= am_running_oss($::am_os_type)
notify { "=*=*= amRunningOS <|:|> ${h} =*=*=*=*=*=*=*=": }
(am_os_type is a fact, that returns win, mac or linux based on the node type)
I was expecting to see Jaguar or Slackware as the return value but I get undefined instead. Does anyone know what am I doing wrong? Is there anything still am I missing in terms of passing the args to the function? Cheers!!
Replying to my own question, in case google lands someone here looking for the same. The thing worked for me is to is define cls like this:
cls = args[0].to_sym if args[0].is_a? String
Cheers!!

Clear input field and enter new info using Watir? (Ruby, Watir)

Pretty positive you have to use .clear, or maybe not as it doesn't seem to be working for me, maybe i'm just implementing it wrong I'm unsure.
Example:
browser.div(:id => "formLib1").clear.type("input", "hi")
Can anyone tell me how to simply clear a field then enter in a new string?
Assuming we are talking about a text field (ie you are not trying to clear/input a div tag), the .set() and .value= methods automatically clear the text field before inputting the value.
So one of the following would work:
browser.text_field(:id, 'yourid').set('hi')
browser.text_field(:id, 'yourid').value = 'hi'
Note that it is usually preferred to use .set since .value= does not fire events.
I had a similar issue, and, for some reason, .set() and .value= were not available/working for the element.
The element was a Watir::Input:
browser.input(:id => "formLib1").to_subtype.clear
after clearing the field I was able to enter text.
browser.input(:id => "formLib1").send_keys "hi"
I had a similar issue, and, for some reason, .set() and .value= were not available for the element.
The element was a Watir::HTMLElement:
[2] pry(#<Object>)> field.class
=> Watir::HTMLElement
field.methods.grep /^(set|clear)$/
=> []
I resorted to sending the backspace key until the value of the field was "":
count = 0
while field.value != "" && count < 50
field.send_keys(:backspace)
count += 1
end
field.send_keys "hi"

DataMapper first_or_create doesn't work... any ideas why?

Well, I've finally decided that I'm not crazy. So, that leaves DataMapper.
Here's what I'm doing. I have a model Msrun which has 1 Metric.
tmp = Msrun.first_or_create # I'll skip the boring details
tmp.metric = Metric.first_or_create( {msrun_id: tmp.id}, {metric_input_file: #metricsfile} )
p tmp.metric # => #<Metric #metric_input_file=nil #msrun_id=1>
tmp.metric.metric_input_file = #metricsfile
p tmp.metric # => #<Metric #metric_input_file=#<Pathname:/home/ryanmt/Dropbox/coding/rails/metrics_site/spec/tfiles/single_metric.txt> #msrun_id=1>
So, why doesn't this work? I'm reading http://datamapper.org/docs/create_and_destroy and doing what it shows working. This has been terribly arduous. Thanks for any help.
Update:
I still can't figure out what is going on, but to prove I'm not insane...
puts Metric.all # => []
tmp.metric = Metric.first_or_create( {msrun_id: tmp.id}, {metric_input_file: #metricsfile} )
puts Metric.all # => [] #??????????????
tmp.metric.metric_input_file = #metricsfile
p tmp.metric # => #<Metric #metric_input_file=#<Pathname:/home/ryanmt/Dropbox/coding/rails/metrics_site/spec/tfiles/single_metric.txt> #msrun_id=1>
tmp.metric.save
puts Metric.all # => [#<Metric #metric_input_file=#<Pathname:/home/ryanmt/Dropbox/coding/rails/metrics_site/spec/tfiles/single_metric.txt> #msrun_id=1>]
So, not only is first_or_create not delivering on the behavior I expect by reading the source
def first_or_create(conditions = {}, attributes = {})
first(conditions) || create(conditions.merge(attributes))
end
but it is also not even creating.
I'm probably missing something here (more of those boring details might help) but if the metric exists, it's metric_input_file shouldn't be updated, i.e., it's only set when new. If you're after updating then you can do
.first_or_create(msrun_id: tmp.id).update(metric_input_file: #metricsfile)
Or if not hitting the database twice is relevant, then
m = Metric.first_or_new(msrun_id: tmp.id)
[set..save..assign]
But if it's not being set on new models, I don't see what would cause that from the code posted so far, more..?
[UPDATED]
Based on your new code, I'd say this is "a classic case" of a false DM save. I usually add the following line to an initialization section, e.g., application.rb in Rails.
DataMapper::Model.raise_on_save_failure = true
Unfortunately, the exception raised never tells you why (there's a special place in hell for that choice, right next to people who talk in theaters.) But it's typically one of:
a slightly incorrect association definition
a has/belongs_to that isn't "required: false" and isn't set
putting the wrong datatype into a field, e.g., a string into a decimal
a validation failing
If you want to post your model definitions, the problem may be spottable there.
In addition to the answer above, I've seen this call die (like, literally halt all execution) with no error when I was doing a find_or_create that would have created an object that violated the primary key constraint. This is because the datamapper model was not in sync with the actual database schema.

ruby one-liner for this possible?

Any chance the 2nd and 3rd lines can be combined in an one-liner and hopefully save one valuable?
def self.date_format
record = find_by_key('strftime')
record ? record.value : "%Y-%b-%d'
end
the above function in a Config model try to fetch a database record by a key, return a default if not found in database.
Even better if can be written in named scope. Thanks
As requested.
Nobody yet has mentioned try, which is perfect for this situation:
value = find_by_key('strftime').try(:value) || "%Y-%b-%d"
You could use:
(find_by_key('strftime').value rescue nil) || "%Y-%b-%d"
though using exceptions is not very efficient.
Does
value = find_by_key('strftime') || "%Y-%b-%d"
work for you?
Do you need to assign a "value" variable at all? If not...
def self.date_format
find_by_key('strftime') || "%Y-%b-%d"
end

Resources