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 trying to make a random phrase generator, which is made of random words.
I have some arrays with words and some with sentences.
Here's the code:
noun = ['noun1','noun2','noun3'...]
#noun = noun.shuffle.sample
verb = ['verb1','verb2','verb3']
#verb = verb.shuffle.sample
... # here are some more words
phrase = [['#noun','#verb'...],['#verb', '#noun'...],[...] ...] # here're some phrases
#phrase = phrase.shuffle.sample
Here's the verb-fragment: <%= #phrase %>
The output isn't rendering array elements, just their names:
["#noun", "#verb", ...]
You're using single quotes. Try this instead:
phrase = [[#noun, #verb...]
What you were doing was outputting a string rather than the variable. Have a look here to find out more about strings and how to use them.
You must remove the quotes in the phrase line:
phrase = [[#noun,#verb], [#verb, #noun]]
Because you want to have an array with the values from the variables #noun and #verb, and not an array with strings containing the words '#noun' and '#verb'
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
str = "Hello my name is __john__ and i am __30__ years old"
str.scan(/__(.*?)__/)
OUTPUT: [["john"], [30]]
Expected result: ["john", 30]
As #aspend mentioned above you will get result just by using .flatten property of array class:
<% str = "Hello my name is __john__ and i am __30__ years old"%>
<%=str.scan(/__(.*?)__/).flatten %>
Preview:
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 6 years ago.
Improve this question
When you make a string...what does it mean to "format" that string?
CHALLENGE: Explain this to me like I'm an absolute idiot. Like, take it to a condescending level. Be mean about it. I'm talking how you would explain a lemonade stand to a very, very stupid child.
The most common use of the phrase refers to the replacing of variable placeholders within a string with the correct string representations of the variables' contents.
Consider:
temp_reading = 25.67528
puts "It is currently %0.1f degrees" % [temp_reading]
-> It is currently 25.7 degrees
String formatting is what turns the template into the string you see in the output.
As pointed out by Phil Taprogge, typically formatting a string refers to changing the representation of data for presentation reasons.
Phil's Example
long_number = 1.11111111111111111111111111111111111111111111111111111111111
puts "%0.1f" % long_number
=> 1.1
puts "%d" % long_number
=> 1
There is tons of documentation on string formatting and typically it can carry over to different languages since this come from the C programming language printf.
Formatting a string could refer, however, to any and all transformations to a string for presentation.
str = "hello world"
str.downcase
=> "hello world"
str.upcase
=> "HELLO WORLD"
str.capitalize
=> "Hello world"
str.titlieze
=> "Hello World"
str.parameterize
=> "hello-world"
https://blog.udemy.com/ruby-sprintf/
http://www.cplusplus.com/reference/cstdio/printf/
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
Is there a way to create a regex for initials without using back references? For example, say I want initials for
New.York
And the regex to output
N.Y. (or n.y)
So far I have the following:
.\.[a-zA-Z]+
This outputs the last the initial of the first word instead of the first initial: w.y.
UPDATE**
I'm also assigned the RegExp to variable and using the =~ to test some things.
You could remove all the lowercase letters using gsub function,
irb(main):004:0> str = "New.York"
=> "New.York"
irb(main):006:0> str.gsub(/[a-z]+/, "")
=> "N.Y"
A ruby way to do this given your input of "New.York" could be:
str.split('.').collect { |s| s[0] }.join('.')
which would return 'N.Y'
Use this regex and you should only output the groups \1 and \2.
([a-zA-Z])[^.]*\.([a-zA-Z]).*?\b
DEMO
If you want to do a replacement you should use \1.\2
You could use the capital letters to dictate the regex match using something like this:
[15] pry(main)> str
=> "New.York"
[16] pry(main)> str.scan(/[A-Z]+/).join('.')
=> "N.Y"
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 got coming string from request like:
1 Ghanaian Cedi = 155.15541 Zimbabwe dollar
I want to have 155.15541 in double. How can I do what?
I hear about predicate, can it help here?
If you'd like to parse this string into components separated by the blank spaces use
NSArray *components = [string componentSeparatedBy:#" "];
to get an NSArray in which each index contains each group of characters. In this particular case, your string would return an array containing:
[#"1", #"Ghanaian", #"Cedi", #"=", #"155.15541", #"Zimbabwe", #"dollar"]
If the desired double is always at index 4, you can convert the corresponding string to a double using
double result = [[components objectAtIndex:4] doubleValue];
If the double isn't always at the same index, maybe you could use a regex to identify the double(s).
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
Here's my code:
1. "special_filter,|filter_str,(&(a=1)(c=11)(p=c=11,o=m,d=4))"
2.{ "a" =>"1", "c" => "11" , "p" => "c=11,o=m,d=4"}
#!/usr/bin/ruby
string = "special_filter,|filter_str,(&(a=1)(c=11)(p=c=11,o=m,d=4))"
hash = {}
string.slice(/\(&.*\)/).split(")").each do |match|
match.tr("(&","").split("=",2).each_slice(2) { |key, value| hash[key] = value }
end
Line by line:
Line 1: Set a variable, string, with the starting string.
string = "special_filter,|filter_str,(&(a=1)(c=11)(p=c=11,o=m,d=4))"
Line 2: Set a variable, hash, with an empty hash to fill.
hash = {}
Line 3:
Cut out the portion of the string that matches this regexp
string.slice(/\(&.*\)/) => "(&(a=1)(c=11)(p=c=11,o=m,d=4))"
The regexp is bookended with forward slashes (/regexp goes here/).
Parentheses have special meaning in regex, so they must be escaped with backslashes.
The & matches the & in the string.
In regex, a . means any character.
* means none to unlimited of the preceding character.
So this regex matches (&) as well as (&fjalsdkfj).
Split the string by right parentheses
string.slice(/\(&.*\)/).split(")") => ["(&(a=1", "(c=11", "(p=c=11,o=m,d=4"]
Then iterate through the array of results
string.slice(/\(&.*\)/).split(")").each do |match|
Line 4:
Take the iteration and remove unwanted characters from it
match.tr("(&","")
Split it one time, using the first = sign
match.tr("(&","").split("=",2)
Use the 2 value array as a key and value on the hash
match.tr("(&","").split("=",2).each_slice(2) { |key, value| hash[key] = value }
My try to this.
Hash[*string[/\&.*/].tr("&(","").split(")").map{|i| i.split("=",2)}.flatten]
Some ideas taken from #Conner's solution ;)
Thanks #corner I was able to know some functions i never used before.