What are -- Ruby Strings called? And how do I insert variables in them?
That syntax is for declaring a HERE DOCUMENT
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc
There's a line-oriented form of the string literals that is usually
called as `here document'. Following a << you can specify a string or
an identifier to terminate the string literal, and all lines following
the current line up to the terminator are the value of the string. If
the terminator is quoted, the type of quotes determines the type of
the line-oriented string literal. Notice there must be no space
between << and the terminator.If the - placed before the delimiter, then all leading whitespcae
characters (tabs or spaces) are stripped from input lines and the line
containing delimiter. This allows here-documents within scripts to be
indented in a natural fashion.
Regarding interpolation, the link gives more details, but it is like a double quoted string if your string is delimited as below (ignore this page's color formatting)
<<-HERE
I can interpolate #{foo}
HERE
whereas it is like a single quoted string
<<-'HERE'
This will print out #{foo} as text
HERE
Also the original pickaxe is a good source http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
Ruby: Merging variables in to a string
The idiomatic way is to write something like this:
"The #{animal} #{action} the #{second_animal}"
Note the double quotes (") surrounding the string: this is the trigger for Ruby to use its built-in placeholder substitution. You cannot replace them with single quotes (') or the string will be kept as is.
Ruby strings with embedded variables
str = "Hi %{name}, %{msg}. Bye %{name}." #yaml it, de-yaml it back to string
h = {:name=> "John", :msg=> "this message is for you"}
puts str % h
#=>Hi John, This message is for you. Bye John.
How to insert variable into formatted text in ruby
Just use uppercase "Q"
variable = "some string"
%Q{ My math #{1+1} and string #{variable}}
Ruby -- use a string as a variable name to define a new variable
There is no way to define new local variables dynamically in Ruby.
It was possible in Ruby 1.8 though with eval 'x = 2'
.
You can change an existing variable with eval
or binding.local_variable_set
.
I would consider using hash
to store values.
How to create and use variables dynamically named by string values in Ruby?
Instance variables are used define properties of an object.
Instead you can achieve through the method send and string interpolation.
Try the below:
def click_colour_cell(colour)
send("has_#{colour}_colour_cell?")
send("#{colour}_colour_cell").click
end
About Send:
send
is the method defined in the Object
class (parent class for all the classes).
As the documentation says, it invokes the method identified by the given String or Symbol. You can also pass arguments to the methods you are trying to invoke.
On the below snippet, send will search for a method named testing
and invokes it.
class SendTest
def testing
puts 'Hey there!'
end
end
obj = SendTest.new
obj.send("testing")
obj.send(:testing)
OUTPUT
Hey there!
Hey there!
In your case, Consider the argument passed for colour
is blue
,
"has_#{colour}_colour_cell?"
will return the string"has_blue_colour_cell?"
and send will dynamically invoke the method named has_blue_colour_cell?
. Same is the case for method blue_colour_cell
Is it possible to insert one variable's value into the name of another variable?
Yes, you can do do this:
if @count < 1001
instance_variable_set("@#{@count}", @count + 1)
end
It would be more idiomatic to store in a hash, e.g.
h = {}
if @count < 1001
h[@count] = @count + 1
end
Related Topics
How to Remove '---' on Top of a Yaml File
The Compiler Failed to Generate an Executable File. (Runtimeerror)
Do I Have to Manually Uninstall All Dependent Gems
Notices for Sequence After Running Migration in Rails on Postgresql Application
Is There a Way in Ruby 1.9 to Remove Invalid Byte Sequences from Strings
Homebrew Installation on MAC Os X Failed to Connect to Raw.Githubusercontent.Com Port 443
Sinatra Configuring Environments on the Fly
How to Express Infinity in Ruby
Is Ruby Really an Interpreted Language If All of Its Implementations Are Compiled into Bytecode
Create Custom HTML Helpers in Ruby on Rails
Check If a Constant Is Already Defined
How to Solve Insecure World Writable Dir /Usr in Path,Mode 040777 Warning on Ruby
Before/After Suite When Using Ruby Minitest
Testing If a Hash Has Any of a Number of Keys