Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Is a simple question about how to print an interpolation on a string in ruby
My method is this:
def hello(parameter)
puts "hello + #{parameter}"
end
puts hello(name)
but irb, show me these message:
NameError: undefined local variable or method `name' for main:Object
It is about my commands to irb? or what i'm doing wrong in my code, 'cause i know that i 've a mistake but can't find where.
Thank you, it's my first post ejje.
The issue lies in your call to hello(name). name in this case is treated at the name of a variable, which you have not defined. If you meant for the console to print "hello + name", then you should call the function with the string "name", surrounded by quote marks: hello("name").
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I'm defining a anonymous class to test a method like so:
let!(:s_integer) do
class << self
def change(value)
self(value)
end
end
def change(value)
self.class.change(value)
end
end
The problem is that where Integer('9') works, self('9') doesn't, but I can't use to_i so how do I work around this problem?
Integer(arg, base=0) is a method in Kernel, not in Integer. So you can't use self to call it.
def change(value)
Integer(value)
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The following shows that when I use the to_f method to covert a string to a floating point number and the last decimal point is dropped. How can preserve all decimal points in a given number?
irb(main):002:0> value='1.7.8'
=> "1.7.8"
irb(main):003:0> value.to_f
=> 1.7
Some context:
I am writing the the value to a file and If I write it as a string I get the quotes '1.7.8'. What I am looking for infact is 1.7.8. Hope that makes sense.
EDIT:
I see the error in my question so I'm trying to close it however I can only vote to close it.
just to clarify what I've found is actually contrary to what I said above.
turns out if I write the string '1.7' to a file it is written as '1.7' but with the string '1.7.8' it is written as 1.7.8. I'm just trying to understand why this is occurring.
To write it to a file simply write it like so:
value = "1.7.8"
File.open("file") { |f| f.puts("#{value}") }
The string in the file will not have quotes around it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Refers to - bizzare way of calling Procs?
#BroiSatse thanks a lot for the answer. One additional moment to clarify - what if one needs to pass extra arguments (added to the code as param_1 and _2):
def callbacks(param_1, param_2, procs)
procs[:var_1].call(param_1)
puts "Proceed"
procs[:var_2].call(param_2)
end
callbacks(arg_1, arg_2, :var_1 => Proc.new {block_1},
:var_2 => Proc.new {block_2})
What goes first? i.e. what will be passed first to execute the def callbacks- arguments (arg_1, arg_2) in place of params (param_1, param_2) or procs (:var_1, :var_2)? This is important to know to properly code the params line - def callbacks(param_1, param_2, procs).
Thanks in advance for any help.
I can't see how you think this will possibly make a difference here, but the arguments are evaluated from left to right and in YARV are pushed onto the stack in that order. Obviously, though, they are all passed before the called method starts executing.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why doesn't this statement work?
1>2 ? puts "true" : puts "false"
Here, I find that most ruby operators are like C with () parentheses having high priority. This code In C
1 > 2 ? printf("true") : printf("false")
executes successfully. Why is ruby code not working?
The error indicates that ternary operator has lower priority than method argument. Ruby parses around the first instance of puts method up to:
puts "true"
and looks if there is another argument, which should be preceded by a comma if there is any. But you have a colon continuing:
: puts "false"
which cases a syntax error.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wrote this:
print "Enter your name:"
name = gets
puts "Hello #{name}. Please to meet you."
and the result was like this:
Hello Moemen
. Pleased to meet you
Why is the remainder of the string after the variable continued in another line? I want it to be "Hello Moemen. Pleased to meet you." Am I missing something?
I'm using sublime text 2, and I couldn't get the gets method to let me input data; it just prints the outcome in the console without giving me a chance to input anything. Any idea?
When you use gets, the input is going to be followed by a newline so you will need to strip the newline before printing it out.
print "Enter your name:"
name = gets.chomp
puts "Hello #{name}. Pleased to meet you."
That should solve your problem.
What you report would not happen. It cannot be true. When you get a string by gets, you may get a string like "Moemen\n". Since an input is delimited by "\n", the string has that at the end. When it is interpolated into "Hello #{name}. Please to meet you.", you would get:
Hello Moemen
. Please to meet you.
but not
Hello Moemen
. Pleased to meet you
as you report. That does not happen.
In order to get "Hello Moemen. Pleased to meet you.", you need to change the string to "Hello #{name.chomp}. Pleased to meet you.".