Ruby Console Input Halting at 1024 Characters

Ruby console input halting at 1024 characters

Try this:

gem install rb-readline

In your script:

require 'rb-readline'

and then, to read a line:

a = Readline::readline

Does that solve it?

If so, you may also want to look at compiling your Ruby with readline:

https://github.com/guard/guard/wiki/Add-Readline-support-to-Ruby-on-Mac-OS-X

make ruby script in console keep accepting input until you're done

Yes,, you can try as something like below using while keyword :

puts "Give your inputs"

while (a = gets.chomp) != 'exit'
puts a
end

# or use until as below
until (a = gets.chomp) =~ /(?:ex|qu)it/i
puts a
end

While you will enter the string 'Exit'/'exit'/'Quit'/'quit' from the command prompt, your while loop will be stopped, Otherwise you will be keep prompting for the next input.

iOS10 NSLog is limited to 1024 chars strings

try printf then instead of NSLog like,

   printf("%s", [string UTF8String]);

It may works

How Do You Clear The IRB Console?

On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.

How to read lines of a file in Ruby

I believe my answer covers your new concerns about handling any type of line endings since both "\r\n" and "\r" are converted to Linux standard "\n" before parsing the lines.

To support the "\r" EOL character along with the regular "\n", and "\r\n" from Windows, here's what I would do:

line_num=0
text=File.open('xxx.txt').read
text.gsub!(/\r\n?/, "\n")
text.each_line do |line|
print "#{line_num += 1} #{line}"
end

Of course this could be a bad idea on very large files since it means loading the whole file into memory.

Making the Calculator

Your while loop should look like this:

do
{
Console.WriteLine("Enter your Operator: ");
ope = Console.ReadLine()?.Trim();
} while (!OperatorToUse.ContainsKey(ope));

In the example below, instead of List I'm using Dictionary but the idea is the same.

In order to avoid if-statements for each operator, you should use a Dictionary and add each operator as a Key and the respective math function as a Value. You can do this with a delegate as in the example. Then you just call the respective function by retrieving it from the dictionary with the selected operator like this:

OperatorToUse[ope](userInput, userInput2)

internal class Program
{
delegate double MathOperation(double a, double b);
static void Main(string[] args)
{
Dictionary<string, MathOperation> OperatorToUse = new Dictionary<string, MathOperation>();
OperatorToUse.Add("+", (a, b) => a + b);
OperatorToUse.Add("-", (a, b) => a - b);
OperatorToUse.Add("/", (a, b) => a / b);
OperatorToUse.Add("*", (a, b) => a * b);
OperatorToUse.Add("%", (a, b) => a % b);
OperatorToUse.Add("^", Math.Pow);

double userInput;
double userInput2;
string ope = String.Empty;
do
{
Console.WriteLine("Enter Your first number: ");
} while (!double.TryParse(Console.ReadLine(), out userInput));

do
{
Console.WriteLine("Enter your Operator: ");
ope = Console.ReadLine()?.Trim();
} while (!OperatorToUse.ContainsKey(ope));

do
{
Console.WriteLine("Enter Your Second number: ");
} while (!double.TryParse(Console.ReadLine(), out userInput2));

Console.WriteLine($"{OperatorToUse[ope](userInput, userInput2)}");
}
}

How to stop C++ console application from exiting immediately?

Edit: As Charles Bailey rightly points out in a comment below, this won't work if there are characters buffered in stdin, and there's really no good way to work around that. If you're running with a debugger attached, John Dibling's suggested solution is probably the cleanest solution to your problem.

That said, I'll leave this here and maybe someone else will find it useful. I've used it a lot as a quick hack of sorts when writing tests during development.


At the end of your main function, you can call std::getchar();

This will get a single character from stdin, thus giving you the "press any key to continue" sort of behavior (if you actually want a "press any key" message, you'll have to print one yourself).

You need to #include <cstdio> for getchar.

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.



Related Topics



Leave a reply



Submit