This code form RubyMonk works in RubyMonk:
class Calculator
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
end
I copied it to Sublime Text 2, set the build system to Ruby, then I saved it. When I type in the console something like add(1, 2), I get the following exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'add' is not defined
I couldn't find a solution to this. I tried reinstalling Sublime Text 2, tried the beta of ST3, tried other code which I know to be good (same error), etc., and nothing is working. Any ideas?
EDIT: On the suggestion of another user, I tried:
Calculator.new.add(1, 2)
which returned:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'Calculator' is not defined
Sublime Text 2 console is python console, not ruby.
Additionally your ruby code is incorrect.
I have the sensation that you're calling the method add(1,2) for no Object at all. What I mean is, in this case, that you're not telling the Calculator to add, but just to the thin air... which does not know how to add!
Try doing th following:
Calculator.new.add(1,2)
And you'll be asking an instance of the Calculator to add.
Tell me if you have any doubts or if I'm completely mistaken!
EDIT: As stated by texasbruce, ST2 console is Python. Therefore, the solution is the following:
Add puts Calculator.new.add(1,2) in the code and hit Ctrl+B to launch Ruby.
Related
So I'm writing a simple Ruby program, essentially a simple ORM. On my "delete" method, I have written the following:
file = "/Users/john/Projects/csv-orm/20180922-test.csv"
def delete id
counter = 0
csv = []
CSV.foreach(file) do |row|
counter += 1
if counter != id
csv << row
end
end
counter = 0
CSV.foreach(file, "w") do |row|
row = csv[counter]
end
end
delete 2
...and I'm running it in an irb session on my terminal (Mac OS X 10.13, ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]) to test it (for example, by pasting in the code above, and then trying delete 2, where the number 2 is the argument passed in for the id parameter), and I get the following error:
Traceback (most recent call last):
4: from /Users/apickle/.rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>'
3: from (irb):23
2: from (irb):19:in `delete'
1: from /Users/apickle/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/csv.rb:1139:in `foreach'
ArgumentError (wrong number of arguments (given 2, expected 1))
Just to get it out of the way, I have tried testing it in a freshly exited and reopened irb session, and I am typing in require "csv" when I start it up. I'm not sure what to do. :/
EDIT: I've renamed the function to destroy and flfllgpt, and I get the same result - and I have made a simple function that takes a single integer named id and putss it. That function works. It seems to be somehow related to the CSV calls... when I comment those blocks out, it doesn't throw that to me. But I've used them before, too!
It is hard to diagnose the problem without the exact error message, which includes the line number where the error occurs, the method in which the error occurs, the method call for which the error occurs, and the stack trace.
However, there is exactly one place in the code you posted where a method is called with 2 arguments:
CSV.foreach(file, "w") do |row|
# …
end
And indeed, the documentation for CSV::foreach clearly says that CSV::foreach only takes one positional argument, this must be the culprit. There simply is no other place in your code that could possibly raise this exception.
I guess CSV.foreach(file, "w") is the culprit here, because I think what you want to do is open a new file and then write the result? Check this out: https://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html#method-c-foreach
If changing it does not help, can you post the full error message? That makes it easier to identify the error.
I use a lot of special characters from Hun language, and there were no problems previously. Now they all give errors when running the whole script (F9). It still runs perfect when running locally (select + F5).
Fúú='bar'
Traceback:
C:\Users\my name\.ipython\cython\_cython_magic_21a3824690cdb52a9fe6a3fa1c63ee73.pyx:1:1: Unrecognized character
Traceback (most recent call last):
File "<ipython-input-6-dcfab52d0ff4>", line 1, in <module>
runfile('E:/Anyagok/Programozas/Python/projekts/elo/mindennap/untitled0.pyx', wdir='E:/Anyagok/Programozas/Python/projekts/elo/mindennap')
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\spyder\utils\site\sitecustomize.py", line 703, in runfile
ipython_shell.run_cell_magic('cython', '', f.read())
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\IPython\core\interactiveshell.py", line 2131, in run_cell_magic
result = fn(magic_arg_s, cell)
File "<decorator-gen-130>", line 2, in cython
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\IPython\core\magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\Cython\Build\IpythonMagic.py", line 321, in cython
assert len(extensions) == 1
TypeError: object of type 'NoneType' has no len()
If I change to e.g.
Fuu='bar'
it works great. Why the sudden change of heart?
EDIT:
Have been messing around with FFMPEG and LIBAV yesterday, because wanted to download and convert Youtube videos to mp3. But I'm pretty sure I ran scripts with these characters succesfully after it.
I am trying to run a Tkinter GUI on Python 3.x and When I use the .get command to get the number off a scale, this error pops up
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Users\Danny\Downloads\Space RPG. Alpha 0.2 (2) (1).py", line 39, in close
print (w1.get(), w2.get())
File "C:\Python34\lib\tkinter\__init__.py", line 2840, in get
value = self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".52674064"
What is happening?
That usually means that you're trying to call a method on a widget that has been destroyed. The string .52674064 is the internal name of a specific widget.
This can easily happen if you call a function via a binding or via after, if the widget is destroyed before the binding or after call has been triggered.
I'm trying to run my python code using sublimeREPL's "Python - RUN current file" command
It works fine if my program has no problems, but when it does, it doesn't show the complete Traceback (I don't get to see the "Repl Closed" message), and the output its not even consistent. Below two runs of the exactly same file (not posting images because stackoverflow doesn't allows me to because I'm new):
First Run:
------- Ford Fulkerson -------
Traceback (most recent call last):
File "Ford-Fulkerson.py", line 282, in <module>
D = FordFulkersonGeneral(G, ['A'], ['E'], None, restricciones)
File "Ford-Fulkerson.py", line 71, in FordFulk|
Second Run:
------- Ford Fulkerson -------
Traceback (most recent call last):
File "Ford-Fulkerson.py", line 282, in <module>
D = FordFulkersonGeneral(G, ['A'], ['E'
I was using the Anaconda's (64 bit) python distribution. Then I changed to a regular python (32 bit) install (made sure the window's path was all right) and even there its not working.
If I run my code from window's terminal I get the full Traceback (the actual error is not important, I know how to fix it):
------- Ford Fulkerson -------
Traceback (most recent call last):
File "Ford-Fulkerson.py", line 282, in <module>
D = FordFulkersonGeneral(G, ['A'], ['E'], None, restricciones)
File "Ford-Fulkerson.py", line 71, in FordFulkersonGeneral
G.deleteNode(v)
File "C:\Users\myusername\Documents\Learning\Anßlisis de Re
des\Ford-Fulkerson\mvr_graph.py", line 196, in deleteNode
self.nodes[node].delete(n)
AttributeError: 'dict' object has no attribute 'delete'
Edit:
I've found the answer by posting this question. The problem was in the path of the file - it contains an accent in the word "Análisis". I changed that and know its working.
It used to work when I had my OS language set to spanish. I set my new installation to english and now it was giving me trouble. I really didn't expected that, shame on you Windows x(.
I don't really know the protocol, I will just leave this question here in case anyone is going through this obscure thing.
I've found the answer by posting this question. The problem was in the path of the file - it contains an accent in the word "Análisis". I changed that and know its working.
It used to work when I had my OS language set to spanish. I set my new installation to english and now it was giving me troubles. I really didn't expected that, shame on you Windows x(.
Doing the Ruby Koans, in the file about_symbols at line 88, I'm not sure of the answer.
This is the code:
def test_symbols_cannot_be_concatenated
# Exceptions will be pondered further farther down the path
assert_raise(what should i put?) do
:cats + :dogs
end
The point of the Ruby Koans is to learn by reading and trying things out.
Open up a terminal and start irb. Then try using the + operator on two symbols. Check the error you get and substitute it as appropriate in the Koans file.
Assuming that your prompt ends in $, that will look something like this:
$ irb
irb(main):001:0> :cats + :dogs
The answer you need will be clear pretty quickly in the error that irb spits out.
To go through step by step in case your completely new to Ruby you could try:
Open up a terminal
Type irb at your prompt to get to the interactive ruby prompt
This is where you can quickly try out different Ruby things
Type the command in question :cats + :dogs
Review the output which will look like
NoMethodError: undefined method `+' for :cats:Symbol
from (irb):1
The name of the exception thrown which is what you are looking for is the first thing e.g. NoMethodError