I am new to ruby and I am reading the SAMS "Teach yourself Ruby in 21
days" book. I've gotten to the part where the book discusses the
operating system and Ruby. I'm having trouble with an example in the
book and was hoping someone could help. The script is spinnertest.rb:
#!/usr/bin/env ruby
require 'spinner2' # Edit this line for different spinners
print "Please wait while I calculate a large Fibonacci number. "
animation = Spinner.new
result = fib(28)
animation.stop
puts
puts result
The class is called spinner2.rb:
class Spinner
Baton = '\|/-'
def initilaize
STDOUT.flush @child = IO.popen('-', 'w+')
if @child.nil?
rotation = 0
until select([$stdin], nil, nil, 0.2)
STDERR.printf "%c\b", Baton[(rotation+=1) &3]
end
gets #read the 'stop' message
STDERR.printf " \b" # erase the last Baton character
exit!
end
end
def stop @child.puts("stop yer rotation") # message content is unimportant @child.close
end
end
When I run spinnertest.rb I get a blank screen for awhile, then after
the calculation is complete I get the following:
../spinner2.rb:18:in `stop': private method `puts' called for
nil:NilClass (NoMethodError)
from ./spinnertest.rb:12
Please wait while I calculate a large Fibonacci number.
I am guessing that ruby is having trouble with 'puts' being used this
way, but I do not understand why, nor, how to fix it. Can someone
please help me with this item?
I am new to ruby and I am reading the SAMS "Teach yourself Ruby in 21 days" book. I've gotten to the part where the book discusses the operating system and Ruby. I'm having trouble with an example in the book and was hoping someone could help. The script is spinnertest.rb:
#!/usr/bin/env ruby
require 'spinner2' # Edit this line for different spinners
print "Please wait while I calculate a large Fibonacci number. "
animation = Spinner.new
result = fib(28)
animation.stop
puts
puts result
The class is called spinner2.rb:
class Spinner
Baton = '\|/-'
def initilaize
^^^^^^
this should be
def initialize
···
STDOUT.flush @child = IO.popen('-', 'w+')
if @child.nil?
rotation = 0
until select([$stdin], nil, nil, 0.2)
STDERR.printf "%c\b", Baton[(rotation+=1) &3]
end
gets #read the 'stop' message
STDERR.printf " \b" # erase the last Baton character
exit!
end
end
def stop @child.puts("stop yer rotation") # message content is unimportant @child.close
end
end
When I run spinnertest.rb I get a blank screen for awhile, then after the calculation is complete I get the following:
../spinner2.rb:18:in `stop': private method `puts' called for nil:NilClass (NoMethodError)
from ./spinnertest.rb:12
Please wait while I calculate a large Fibonacci number.
I am guessing that ruby is having trouble with 'puts' being used this way, but I do not understand why, nor, how to fix it. Can someone please help me with this item?
>Hi-
>
>I am new to ruby and I am reading the SAMS "Teach yourself Ruby in 21
>days" book. I've gotten to the part where the book discusses the
>operating system and Ruby. I'm having trouble with an example in the
>book and was hoping someone could help. The script is spinnertest.rb:
>
>#!/usr/bin/env ruby
>
>require 'spinner2' # Edit this line for different spinners
>
># Our slow recursive Fibonacci function.
>def fib(n) (n<=2) ? 1 : (fib(n-2)+fib(n-1)) end
>
>print "Please wait while I calculate a large Fibonacci number. "
>
>animation = Spinner.new
>result = fib(28)
>animation.stop
>
>puts
>puts result
>
>
>The class is called spinner2.rb:
>
>class Spinner
> Baton = '\|/-'
> def initilaize
>
>
^^^^^^
this should be
def initialize
> STDOUT.flush
> @child = IO.popen('-', 'w+')
> if @child.nil?
> rotation = 0
> until select([$stdin], nil, nil, 0.2)
> STDERR.printf "%c\b", Baton[(rotation+=1) &3]
> end
> gets #read the 'stop' message
> STDERR.printf " \b" # erase the last Baton character
> exit!
> end
> end
>
> def stop
> @child.puts("stop yer rotation") # message content is unimportant
> @child.close
> end
>end
>
>When I run spinnertest.rb I get a blank screen for awhile, then after
>the calculation is complete I get the following:
>
>../spinner2.rb:18:in `stop': private method `puts' called for
>nil:NilClass (NoMethodError)
> from ./spinnertest.rb:12
>Please wait while I calculate a large Fibonacci number.
>
>I am guessing that ruby is having trouble with 'puts' being used this
>way, but I do not understand why, nor, how to fix it. Can someone
>please help me with this item?
>
>:)Thanks.
>
>SA
>
>
>
>
How does one get $: to seamlessly include the directory of the parent script?
I've written a library:
foo.rb
And a script,
dofoo.rb which does 'require "foo"'.
If I run dofoo.rb in it's own directory, require works. If I include it's directory in my path, and run it from another directory, it can't find the foo.rb library.
I've used $:.push("the path to my library") ( e.g. $:.push(File.dirname($0)) ) before the require, which works but seems inelegant.
Ultimately, I'd like to be able to distribute a set of scripts that any user could drop into a path location.
Are there any other ways to do this (assuming no knowledge of the target system)?