Why is Ruby output suppressed on the console? - ruby

# code.rb
def hello
puts "hello"
end
:$ ruby code.rb
Nothing is output on the console! I am using Ubuntu 13.04.
If I run the same code in IRB it works!

You have to call your code, you're just defining a method:
# code.rb
def hello
puts "hello"
end
hello
$ ruby code.rb

You define a method, but you're never calling it. Try this:
# code.rb
def hello
puts "hello"
end
hello
Run it:
:$ ruby code.rb

You need to call the method, in this case, hello in the script:
def hello
puts "hello"
end
hello

Related

Make Thor show message for top level command

Is there any way to make Thor show a general message for the top level command?
$my_command help
I'd like to show a welcome message here.
Commands:
my_command help [COMMAND]
The closest thing I can think of is adding a default task and using it to invoke the help task. You'd get this message when calling $my_command with no arguments
require 'thor'
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello #{name}"
end
desc "greeting", "this is just a greeting"
def greeting
puts "Welcome to MyCLI"
invoke :help
end
default_task :greeting
end
MyCLI.start(ARGV)
# $my_command
# output:
# Welcome to MyCLI
# Commands:
# test.rb greeting # this is just a greeting
# test.rb hello NAME # say hello to NAME
# test.rb help [COMMAND] # Describe available commands or one spec...

How do i run Ruby script from command line ?

I've a file, which I can run from command line using:
ruby filename.rb
Which outputs:
12345
A different script containing:
def hi()
puts "hello"
end
does not return anything when I run it from the command-line.
How do I run this from the command line?
Add puts hi("John") to the bottom of the method:
def hi(name)
puts "hello"
end
puts hi("John")
Where "John" is whatever name you want it to be.
Then just run it as usual, ruby yourfilename.rb
Try putting this in filename.rb:
def hi()
puts "hello"
end
hi
Then run your code in the command line: with ruby filename.rb

ruby - pipe-in from one class to another

i have a class named Foo, which uses pipe-in to read input from the command line, and it works well. i have another class named Bar, which invokes Foo, and has to feed (pipe-in) Foo in the same manner that Foo expects, but it does not seem to work for me.
see my snippet below.
i would appreciate any help.
note:
i know i can avoid doing so by passing object data from Bar to Foo, but i would like to use the pipes.
$ ls -x1
bar.rb
foo.rb
test.rb
$ cat *
# bar.rb
require "stringio"
class Bar
def self.pipe
input = StringIO.new
input.write "bar"
input.rewind
$stdin = input
Foo.print
$stdin = STDIN
end
end
# foo.rb
class Foo
##stdin = STDIN.tty? ? nil : $stdin.read #ok for cli pipe-in
def self.print
puts "stdin: #{##stdin}"
end
end
# test.rb
$:.unshift File.join(File.dirname(__FILE__))
require "foo"
require "bar"
Bar.pipe
$ echo "piped" | ruby test.rb
stdin: piped
$ ruby test.rb
stdin:
what is being done wrong, and why? a solution would be great.
i found my error. i modifying Foo as follow, but i broke its functionality.
$ cat foo.rb
# foo.rb
class Foo
def self.print
##stdin = STDIN.tty? ? nil : $stdin.read #ok for cli pipe-in
puts "stdin: #{##stdin}"
end
end
$ echo "piped" | ruby test.rb
stdin: bar

Run def in ruby program

I'm a beginner in programming and wrote this little program:
Test.rb:
# encoding: utf-8
require 'open-uri'
require 'nokogiri'
def parse_file
doc = Nokogiri::XML(File.open("test.xml"))
parse_xml(doc)
end
def parse_xml(doc)
doc.root.elements.each do |node|
parse_tracks(node)
end
end
def parse_tracks(node)
if node.node_name.eql? 'kapitel'
puts 'New Kapitel'
end
end
I know how to execute this code:
ruby test.rb
But how can I call the def parse_file?
Simply add whatever you want to the end of your file. Ruby scripts are simply scripts, they are being interpreted:
…
end
parse_file # ⇐ HERE YOU GO
You can either call the method at the end of your test.rb file:
def parse_file
# ...
end
parse_file
And run it with
$ ruby test.rb
Or leave the file as it is, require it as a library and call the method manually:
$ ruby -r test.rb -e "parse_file"
Rather than hard-coding your file path, you can pass it as an argument when calling your script. Arguments can be accessed via the ARGV array:
def parse_file(file)
doc = Nokogiri::XML(File.open(file))
parse_xml(doc)
end
parse_file(ARGV.first)
Now you can run it with:
$ ruby test.rb test.xml
Another option is to make the script executable. Add a shebang as the first line of you file:
#!/usr/bin/env ruby
And set the execute flag:
$ chmod +x test.rb
Now you can run it with:
$ ./test.rb test.xml
just add
parse_file
in the end of your ruby file

How can I get "thor list" to list methods in ruby class

I have problems listing my tasks in thor, even with the simplest program:
class Mytest < Thor
desc "Hello world", "Puts 'hello world' on the console"
def hello
puts "Hello world"
end
end
Here is the console output when i run thor list
$ thor list
mytest
------
thor mytest:world # Puts 'hello world' on the console
As you can see the "hello" method name is omitted from the output. Can you help me out please?
Thank you,
Paul
Just change the 'desc' line to:
desc "hello", "Puts 'hello world' on the console"
Hope it helps.

Resources