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'm working on a project in RMXP with Ruby scripts.
I create a table with id maps. I am now looking to go the table such that if the player enters one of the maps of the table, things happen.
Example of action: tMaps = [006, 008, 009]
If the hero is on the map 004, 005 ... it nothing happens If the hero
is on the map 006, Action
I hope this is feasible. If not, what other means I have in Ruby to find the maps and to assign actions?
PS I've traveled a bit, this wiki : http://fr.wikibooks.org/wiki/Programmation_Ruby/Contr%C3%B4le
Your list is wrong, I suppose you want integer : [6, 8, 9] not [006, 008, 009] (this is octal form)
tMaps = [6, 8, 9]
tMaps.each do
|value| puts 'value : '+value.to_s
end
# or :
for value in tMaps
puts 'value : '+value.to_s
end
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 5 years ago.
Improve this question
Rearrange the key values in the hash below to look like:
{"abc"=>"wqeq","dfg"=>"sadsada","qwe"=>"asdad","yui"=>"asdasd","abc"=>"weqqw","qwe"=>"assadsad","yui"=>"asd","dfg"=>"asdsad"}
{"abc"=>["wqeq","weqqw"] ...}
Thanks
The problem here is that assigning a key with the same value as a previous one overwrites it.
As far as I'm aware, there's no code-based solution to solve this; you just need to re-write it with arrays as the keys' values where they're duplicated.
{"abc" => ["wqeq", "weqqw"],
"dfg" => ["sadsada", "asdsad"],
"qwe" => ["asdad", "assadsad"],
"yui" => ["asdasd", "asd"] }
I guess that's the output you're looking for. If not, please add a little more info to the questions and I / others will be able to help out.
If you want to bring a gun to a knife fight, you can use compare_by_identity:
hash = {}
hash.compare_by_identity
hash["abc"] = "wqeq"
hash["abc".dup] = "weqqw"
# ... etc ...
puts hash # => { "abc" => "wqeq", "abc" => "weqqw" }
... and then process that. Feels kinda dirty to me though.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What would be the right way to find the location of a character within the alphabet? For example:
"A".find_score # => 1
"C".find_score # => 3
"A".ord
returns 65, the numeric code for "A", which is what the alphabet starts at. If you want it to start at 1 you could just subtract 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
which works like:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3
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 use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
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 9 years ago.
Improve this question
Here is my problem:
Given a multi dimensional array: [['monday', 'saturday'], ['beginner'], ['kid', 'adult']]
I want:
['monday', 'monday-beginner', 'monday-beginner-kid', 'monday-beginner-adult', 'monday-kid', 'monday-adult',
'saturday', 'saturday-beginner', 'saturday-beginner-kid', 'saturday-beginner-adult', 'saturday-kid', 'saturday-adult',
'beginner', 'beginner-kid', 'beginner-adult',
'kid',
'adult']
Here are questions I saw that could help:
Creating permutations from a multi-dimensional array in Ruby
How to combination/permutation in ruby?
first, *rest = [['monday', 'saturday'], ['beginner'], ['kid', 'adult']]
.map{|a| [nil, *a]}
first.product(*rest).map{|a| a.compact.join("-")} - [""]
I've a solution in 2 steps :
- Find all the possible combinaisons, 4 in your examples:
['monday', 'beginner', 'kid']
['monday', 'beginner', 'adult']
['saturday', 'beginner', 'kid']
['saturday', 'beginner', 'adult']
Then use combinaison method on it 3 times using n from 1 to 3 and then, merge the 3 returns. You'll finally just have to call .join("-") on each permutation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this:
array = ["a","b","c"]
How do I get this:
"a","b","c"
I need to get the items out of the array, each double-quoted, separated by a comma.
array.collect { |a| "\"#{a}\"" }.join(",")
I'm just started to learn ruby, I guess:
return ["a","b","c"].map{|i| '"' + i + '"'}.join(",")
Might you want to get this:
irb(main):009:0> [1, 2, 3].map(&:to_s).join('","')
=> "1\",\"2\",\"3"
"a","b","c" this is not abject (these are 3 objects). But in ruby any code returns object value. So you should know what you want to get: 1 object (I returning string in this example) or various. If you want to get 3 objects you should extract array like this:
a, b, c = [1,2,3].map(&:to_s)