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
I have an array within an array. I can call a certain object by doing this...
array[1][2]
I would like to be able to insert a variable instead of one of these values. Maybe something like...
array[#{variable1}][#{variable2}]
Is this possible?
Just replace the numeric literal with the variable:
variable1 = 1
variable2 = 2
array[variable1][variable2]
What you mentioned is known as string interpolation, but can only be used in quotes.
For example, say you have a variable defined, price
price = 80
You can say something like this
puts "The price is $#{price}"
This will translate to The price is $80
It's not possible to do that in arrays, but it is possible to substitute values inside
a = 7
b = 9
array[a] will get the 8th value of the array, whereas array[b] will get the 10th value of the array.
Hope this helps.
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 6 years ago.
Improve this question
I was wondering if there was a way to reference an object when assigning it to a variable. Here is an example of where this question would apply:
Let's say I wanted to assign the substring of a regex to a variable, call it i.
To assign, I could write
i = /some_regex/.to_s
and then
i = i[3...i.length]
I could also write it all in one line, like
i = /some_regex/.to_s[3.../some_regex/.to_s.length]
However, both of these examples seem somewhat redundant and the second approach could become unwieldy with big regex's or multiple method calls. Is there a way to reference the object being changed without having to rewrite everything?
Edit: Sorry for previous ambiguity.
Ruby evaluates the right side of the equals sign before setting the left side equal to it, so if i already exists you can do what you're talking about. A simple example:
i = 10
i = i + 1 # now i = 11
However, you can't use i to define itself. You could use the following two lines:
i = expression.match(/\d+[\+|-|\*|\/]/)
i = i[0..i.length - 1] # Note: this is the same as i = i[0...i.length]
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 6 years ago.
Improve this question
I capture the text value and store it in a variable. Now I want to use the specific characters from the variable. How can I do it in Ruby?
var = "ABC-DEF-XYZ"
I want to use "DEF" value from the variable and want to skip the rest.
You can capture the 'y' value from your string using one of these 2 methods:
Regex
y = input_value.gsub(/^\w\w\w-(\w\w\w)-\w\w\w$/, "\\1")
Split
y = input_value.split('-')[1]
Once you have the 'y' value from your input, you can compare it to the table list.
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 want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.
As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.
Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
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
I have two arrays.
One is named foods, it is an array of strings.
The other is named dataList. It is an array of objects that contain an attribute named name.
What I need to do is check if the names in the array foods is present in the dataList. So that means all the items in foods needs to be equal to one of the name attributes in dataList.
I know how to check arrays if both arrays contained the same type of data. But I'm not sure how to do this.
Since we don't care about the order of the elements nor about the number of times an element is present, we can do this easily with Sets.
require 'set'
foods_set = Set.new(foods)
attribute_names_set = Set.new
dataList.each do |d|
attribute_names_set << d.name
end
return foods_set == attribute_names_set
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
I need a Ruby program that, given a file as parameter, returns a hash or array that gives the number of characters for each line.
How can I do this elegantly in Ruby ?
File.open('file_name').map(&:length)
Check this:
File.open('file_name').inject([]) do |counts, line|
counts << line.size
end
Take a note that it will also count new line characters.
For file with content
aa
aaaa
a
the result will be
[3, 5, 1]
If you don't want to count them, check this method String#chomp