What is wrong with this piece of code(trivial) - ruby

This is a very trivial function I wrote to read text as a prompt. It doesn't work but doesn't give me errors either. I've tried all modifications I can think if to get it to work, but to no avail.
Code:
def prompt(query="")
print (query) #I also tried 'print"#{query}"
var = gets()
return var
end
name = prompt("Input your name:")
puts"#{name}"
Nothing happens. This has frustrated me, that I couldn't do something so simple, and I've spent hour on it.
There was no problem with the code itself. It was simply that my editor Sublime Text 3 didn't handle accepting user input well.

How to make scripts run from command line:
Create a file ending in ".rb", for example "my_script.rb"
Run ruby my_script.rb
Don't forget ".rb" ;)

Related

Qt : Reading the text file and Displaying in LineEdit

I have an input file and a batch file. When the batch file is executed using the System command,
a corresponding outfile is generated.
Now I want a particular text (position 350 to 357) from that outfile to be displayed on to my lineedit widget
Here is that part of my code:
system("C:/ORG_Class0178.bat")
Now the outfile will be generated
File.open("C:/ORG_Class0178_out.txt", 'r').each do |line|
var = line[350..357]
puts var
# To test whether the file is being read.
#responseLineEdit = Qt::LineEdit.new(self)
#responseLineEdit.setFont Qt::Font.new("Times NEw Roman", 12)
#responseLineEdit.resize 100,20
#responseLineEdit.move 210,395
#responseLineEdit.setText("#{var}")
end
When I do test whether the file is being read using puts statement, I get the exact required output in editor. However, the same text is not being displayed on LineEdit. Suggestions are welcome.
EDIT: A wired observation here. It works fine when I try to read the input file and display it , however it does not work with the output file. The puts statement does give the answer in editor confirming that output file does contain the required text. I am confused over this scenario.
There is nothing wrong with the code fragments shown.
Note that var is a local variable. Are the second and third code fragments in the same context? If they are in the same method, and var is not touched in-between, it will work.
If the fragments belong to different methods of the same class, than an instance variable (#var) will solve the problem.
If all that does not help, use Pry to chase the problem. Follow the link to find the pre-requisites and how to use. Place binding.pry in your code, and your program will stop at that line. Then inspect what your variables are doing.
try 'rb' instead of 'r'
File.open("C:/ORG_Class0178_out.txt", 'rb').each do |line|
var = line[350..357]
puts var

Can anyone see what is wrong with my code?

My code is as follows:
a=msgbox("Do you like waffles",4+16,"Waffles")
Do While a=vbno
a
Loop
if a=vbyes then
b=msgbox("Do you like pancakes",4+16,"Pancakes")
Do While b=vbno
b
loop
if b=vbye then
c=msgbox("Do you like French toast",4+16,"French toast")
Do While c=vbno
c
Loop
else
d-msgbox("good",0+16,"YAY!")
end if
I know it is basic, but it comes up with the error message: "Error: Expected 'End'"
But as you can see 'end' is at the start of line 17, if it is something I haven't seen some where else in the code that might be causing this. I'm kind of new to this language and was putting things I knew how to do into a semi-useful paten.
I saved it as a .bat file and ran it using cmd.
Here is the original code posted in the question (as I'm writing this):
a=msgbox("Do you like waffles",4+16,"Waffles")
Do While a=vbno
a
Loop
if a=vbyes then
b=msgbox("Do you like pancakes",4+16,"Pancakes")
Do While b=vbno
b
loop
if b=vbye then
c=msgbox("Do you like French toast",4+16,"French toast")
Do While c=vbno
c
Loop
else
d-msgbox("good",0+16,"YAY!")
end if
A main error cause seems to be a notion that one can assign a definition to a variable. Well one can, but not in that way. Here is corrected code:
option explicit
dim answer
do
answer = msgbox("Do you like waffles",4+16,"Waffles")
if answer = vbYes then exit do
answer = msgbox("Do you like pancakes",4+16,"Pancakes")
if answer = vbYes then exit do
answer = msgbox("Do you like French toast",4+16,"French toast")
if answer = vbYes then exit do
loop
call msgbox( "good",0+16,"YAY!" )
There are however umpteen zillion ways to prepare food, and so also with code, plus, it's not clear if the above is the intention, but I guess it's pretty close.
At any rate, it's code that works and that you can build further on
Note that VBScript documentation is now only available as a CHM compiled help file, called "script56.chm".
The VBScript documentation is both available as a handy CHM compiled help file, called "script56.chm", as well as online in the MSDN library.
Note: you need to save the source code as a .vbs file.

Why does gets return zero in Ruby

I have a simple method
def save_logline
print "What's the name of the movie"
movie_name = gets.strip
print "And what is your precious logline?"
logline = gets.strip
File::open(movie_name + '.txt', 'w') do |f|
f.write(logline)
end
end
Anytime I run the code, I receive the first prompt where I insert the name of the movie, but once I press enter the second print is called, giving me this message.
And what is your precious logline?=> 0
The file is written but I'm prevented from inserting anything into the text file. What's wrong with my logic and how do I fix this? I am using irb in RubyMine with Ubuntu.
EDIT: It appears that due to other users response to my code working and my own test on a different development machine my code is just not working on that one machine. The only thing I could say is that I was using RubyMine's irb console. The question now must be is it a RubyMine issue or is there a bigger problem with my dev machine?
FINAL EDIT: I checked on my second comp that has RubyMine, it appears that it does not work in that RubyMine's irb console either. I'm going to issue the bug to RubyMine and give credit to Jeremy in a day or two unless someone knows why RubyMine's irb is acting funky.
Your code works. The 0 is the return value of f.write. It's saying it wrote zero bytes to your file, because you didn't give it any input.
Edit: Maybe you pressed enter twice by accident...

What's a reasonable way to read an entire text file as a single string?

I am sure this is an easy one; I just couldn't find the answer immediately from Google.
I know I could do this (right?):
text = ""
File.open(path).each_line do |line|
text += line
end
# Do something with text
But that seems a bit excessive, doesn't it? Or is that the way one would do it in Ruby?
IO.read() is what you're looking for.
File is a subclass of IO, so you may as well just use:
text = File.read(path)
Can't get more intuitive than that.
What about IO.read()?
Edit: IO.read(), as an added bonus, closes the file for you.
First result I found when searching.
I wanted to change the mode, which doesn't seem possible with IO.read, unless I'm wrong?
Anyway, you can do this:
data = File.open(path,'rb',&:read)
It's also good for when you want to use any of the other options:
https://ruby-doc.org/core/IO.html#method-c-new

Programming TextMate in Ruby. Problem with TextMate.go_to

I'm modding a TextMate bundle even though I'm a complete beginner at Ruby. The problem I'm trying to solve is the issue of moving the caret to a certain positition after the command has made its output.
Basically what happens is this:
I hit a key combo which triggers a command to filter through the document and inserts text at the relevant places, then exits with replacing the document with the new filtered text.
What I want to happen next is for the caret to move back to where it originally was. I was pretty happy when I found the TextMate.go_to function, but I can only get it partly to work. The function:
positionY = ENV['TM_LINE_NUMBER']
positionX = ENV['TM_LINE_INDEX']
...
TextMate.go_to :line => positionY, :column => positionX; #column no worky
I can get the caret to the right line, but the column parameter isn't working. I've tried shifting them about and even doing the function with just the column param, but no luck. I've also tried with a hard coded integer, but the positionX param prints the correct line index, so I doubt there's anything there.
This is the only documentation I've found on this method, but I took a look in the textmate.rb and to my untrained eyes it seems I'm using it properly.
I know this can be achieved by macros, but I want to avoid that if possible.
I also know that you can use markers if you choose "Insert as snippet" but then I'd have to clear the document first, and I haven't really figured out how to do this either without using the "Replace document" option.
Anyone?
Let's look at the source code of the bindings:
def go_to(options = {})
default_line = options.has_key?(:file) ? 1 : ENV['TM_LINE_NUMBER']
options = {:file => ENV['TM_FILEPATH'], :line => default_line, :column => 1}.merge(options)
if options[:file]
`open "txmt://open?url=file://#{e_url options[:file]}&line=#{options[:line]}&column=#{options[:column]}"`
else
`open "txmt://open?line=#{options[:line]}&column=#{options[:column]}"`
end
end
Rather hackishly, the binding sets up a txmt:// URL and calls open on it in the shell.
So the first thing to do would be constructing an open URL and typing it into Terminal/your browser to see if TextMate is respecting the column parameter. If that works then perhaps there is a bug in your version's implementation of Textmate.go_to.

Resources