Why wont this print "Welcome to Ruby!" [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm following the Ruby course on Codeacademy and it's asking me to create a method, named welcome, that puts "Welcome to Ruby!" After defining the method, call it. Here's my code:
def welcome()
puts "Welcome to Ruby!"
end
Why doesn't this work?

You need to actually call the method after:
def welcome
puts "Welcome to Ruby!"
end
# Call it
welcome

What you've done so far is create a function that does something, but you're never calling that function. Functions and methods don't do anything by themselves until called / invoked.
I don't know what this tutorial you're following entails but more than likely you're missing that invocation line, which can probably go at the end of the file that contains this function:
# your function definition here...
welcome()

Related

Ruby Class error output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
can anyone tell me why the last line of my code output nothing? THX
You need to use initialize not init method,
class Mystuff
def initialize
#apple="I am instance"
end
def talk
puts 'I am taking'
end
attr_reader :apple
end
thing = Mystuff.new
thing.talk
puts thing.apple
output
I am taking
I am instance

Parse form using Nokogiri and pass it to URI.encode_www_form? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have simple HTML form, which I got from a web page:
<form id="my">
inputs....
</form>
I need to get this form via it's ID, which I know how to do:
#get_doc = Nokogiri::HTML(page)
nb = #get_doc.at_css('#my')
maybe could i iterate via object ?
I need to get all the input values and input names into some variable, and then pass it to URI.encode_www_form.
How can I do this? How could I get all the inputs inside the form with names and values, and pass them to encode_www_form?
arr = []
# form = doc.at_css '#form'
form.css('input').each do |i|
arr << [i['name'], i['value']]
end
URI.encode_www_form arr

Is it possible to run a script when RSpec finishes a test [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
Ideally I'd like to be able to call one shell script if all the tests pass and a different one if they fail. Any ideas?
Just add an after :suite hook to your configuration:
RSpec.configure do |config|
config.after(:suite) do
# do whatever you need to do
end
end
See before and after hooks in the RSpec documentation.
From the docs:
The rspec command exits with an exit status of 0 if all examples pass,
and 1 if any examples fail.
So a small script like this should work:
if rspec
./success-script
else
./failure-script
fi

Ruby function titled inflect [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is ruby code:
[:val1, :val2, :val3].each do |method_name|
define_method(met_n) do |param1, param2|
inflect(param1, param2, SOME_CONST[met_n.to_s])
end
end
It's not mine code. I tried to figure out what inflect is, but I failed, although it should be a standard ruby function.
So how is it defined or where do I find a documentation about it?
inflect isn't a standard ruby function. This must be part of someone else's API.

When will Ruby's if condition be executed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my latest task, I found some code like this:
if false
print "a"
elsif true
print "b"
else
print "c"
end
Is if else statement correct? Will if ever be executed?
This code will always print "b".
One explanation is that this code was put in as a placeholder for some real logic which was supposed to be added at a later point in time.

Resources