Ruby code does not run on windows command line - ruby

I tried to run this code:
#!/usr/bin/env ruby
class StringSwap
def initialize(str1,str2)
raise Error unless str1.length>4
raise Error unless str2.length>6
temp=str1[3]
str1[3]=str2[5]
str2[5]=temp
#str1=str1
#str2=str2
end
def print()
put str1
put str2
end
end
def main()
puts "WHYYY"
s1=gets().chomp()
s2=gets().chomp()
obj=StringSwap.new(s1,s2)
obj.print()
end
Nothing happens when I type ruby file.rb. It does not give an error but it does not run my program either. When I tried to run scripts like "Hello World" in same way, it worked. Any help would be appreciated

Okay, I was able to solve that by just deleting the main function. Then it worked but I don't know why? Could anybody enlighten me?

Related

Simple Ruby program does not produce output

I wrote a basic ruby program with TextMate in Mac OS:
def hello
puts " This works!"
end
name it Check-it.rb
I open a Terminal session, cd to the directory where the program is stored.
Then I type
ruby Check-it.rb
And nothing appears.
ruby -v
shows me the version, so it's there.
But with this and every other Ruby program, nothing appears.
As others already pointed out. The code in your file
def hello
puts " This works!"
end
defines a method called hello that outputs a string. But that method is never called. To actually call that method and run it change your code in the file to
def hello # this block defines the `hello` method
puts " This works!"
end
hello # this line calls the method `hello`
I think you are not calling this method at all. Call this method and then run your code, it will work.

Not able to get result for def using ruby on mac osx

This is just a sample method I have created for testing purpose using Ruby on Mac OSX 10.12 but I don't get the desired output: Can anyone suggest please? I tried getting the result using both paranthesis and without (). It doesn't even throw any error.
def hi
puts "Hello World"
End
hi
hi()
hi("Hello Matz")`
Try this:
def hi
puts "Hello World"
end
hi
hi()
And this:
def greet(greeting)
puts greeting
end
greet("Hello Matz")
Note that in this line:
hi("Hello Matz")`
you have a tick mark at the end, so that is an error:
1.rb:5: syntax error, unexpected tXSTRING_BEG, expecting end-of-input
It doesn't even throw any error.
Then you aren't running that program.
I suggest you open a Terminal window (Applications/Utilities/Terminal.app), and type in:
$ vimtutor
vim is a free computer programming editor that comes with your Mac. Do the tutorial and learn how to use vim. To run a ruby program, you enter your code into a file, then save it as, say, my_prog.rb. Then you need to give that file to ruby to execute it. You execute a ruby program like this:
$ ruby my_prog.rb
You can create a directory for all your ruby programs like this:
$ mkdir ruby_programs
$ cd ruby_programs
To create a new file inside that directory, use vim:
~/ruby_programs$ vi my_prog.rb
Once you are done typing in your code, save the file, which will put you back at the prompt in Terminal, then you can run your program:
~/ruby_programs$ ruby my_prog.rb
Once you get comfortable with vim, and you feel confident running your ruby programs, consider installing macvim with the vivid chalk color scheme:
It's nicer to look at than plain vim.
Try editing your file so that it reads:
def hi
puts "Hello World"
end
hi
Some important differences to note: def and end are both case-sensitive. The inside of the function definition is indented by two spaces. Since the function takes no arguments, no parentheses are necessary on the call to hi on line 4.
Depending on your filename, enter the command ruby FILENAME and you should see the output Hello World
Ruby keywords are case sensitive. Your code uses End and you probably wanted to use end to mark the end of the hi method.
Because End is not the same as end (and End is not a keyword), irb keeps waiting for input and treats the other three lines as part of the hi method. As far as it can tell, its definition is not complete until it reaches the end keyword (all non-capital letters.)
The correct way to define the method is:
def hi
puts "Hello World"
end
Then you can call it using either hi or hi().
Calling it as hi("Hello Matz") (or hi "Hello Matz") throws an ArgumentError exception with the message wrong number of arguments (given 1, expected 0) because it is called with one argument but the definition of method hi doesn't specify anything about arguments (by its definition, the method hi doesn't accept any argument).

Ruby script - does not print 'Hello world!' from the command line

I have the following code in the script a.rb.
def main
puts "Hello World!"
end
When I run ruby a.rb on the command line, it doesn't display anything.
Why is this happening?
Unlike languages like C/C++/Java, Ruby does't have a main method that's called at program startup. The name main is not special.
You defined a method named main, but never call1 it.
def main
puts "Hello World!"
end
main # here, call the method
1: Technically, calling methods should be called sending messages, the idea comes from Smalltalk.

app academy ruby-test-first spec errors

I'm new to programming and I'm currently learning on my own. I'm following first precourse for App Academy.
I'm having tons of trouble trying to get the first spec to run on the first project 00_hello.
I'm working on a Windows 7 pc 64 bit and I haven't had much issues running Ruby on it till I had to use rspec.
I also have a VM running Mac OS X but I'm having similar issues on that too.
The current error I'm getting when I run 00_hello_spec is
D:/1/spec/00_hello_spec.rb:105:in <main>': undefined method
describe' for main :Object (NoMethodError)
This is my first time using rspec, I really appreciate any help setting it up properly and getting it running.
Here is the content of 00_hello_spec.rb:
require "00_hello"
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end
I had a similar problem with those tests.
First run rspec -v that will tell you if RSPEC is working.
Second change the 'require' line in the file to:
require "./hello"
As the default actually just gives you "hello" and sometimes it can't find the other file because of that. Both files need to be in the same folder for that to work.
If RSPEC is working and you have that line altered it should work. If it doesn't uninstall RSPEC and use a tutorial to re-install it.
Also a lastly when you run it in the terminal make sure you use
rspec hello_spec.rb
If you use the Ruby command it won't work.

RubyFiddle issue - NameError: undefined local variable or method 'gets' for #

class SqrrtProg
def hello
puts "Hello! Welcome to the square root program."
puts "\n Please enter a number: "
number = gets
puts number
end
def Sqrrt
end
end
object = SqrrtProg.new
object.hello
I am simply trying to use 'gets' to get user input. I had read that it might be because, by default, gets tries to read information from a file. I have tried name = $stdin.gets and name = &stdin.gets.chomp etc... However, I end up at the same error.
I am going to answer my own question as it has been solved. The code is fine when run from a terminal. This problem apparently stems from a limitation in RubyFiddle. Hopefully this question can help someone who comes across the same problem with the RubyFiddle enviornment :)

Resources