Tagged Questions
0
votes
4answers
27 views
Ruby on Rails - unless multiple conditions
I'm trying to substitute an expression unless the expression is one of two values.
def substitute_string (string)
string.gsub('abc', 'xyz') unless string == ('dabc' || 'eabc')
end
...
-1
votes
2answers
41 views
Ruby: returning modified string after iteration
So ive been trying to write the pig latin method with its different instances. However, the join method at the end simply joins the original words of the string as opposed to the modified words that ...
1
vote
1answer
30 views
Ruby: Concise way to conditionally join strings with newlines?
I need to add a description string to an existing comments variable, which contains either a string or nil. I want to separate the new description from any existing comments with a newline, but only ...
0
votes
1answer
30 views
How do I convert this string into a hash?
The following code returns some cookie data as a string.
require 'httparty'
r = HTTParty.get('https://mijn.ing.nl/internetbankieren/SesamLoginServlet')
r.headers
# => {"date"=>["Sat, 26 Jul ...
1
vote
1answer
39 views
Why doesn't this “Freeze” the string?
i have the made this:
a = 1280
b = 900
a.freeze
b.freeze
a = 800
b = 600
puts a
puts b
and it results in:
800
600
but the documentation said, it should raise me an error if i try to modify an ...
-3
votes
2answers
54 views
What is the difference bewtween var = 55 and :var = 55 in Ruby? [closed]
I have just been watching this video ion Symbols in Ruby. In the video it seems to say that assigning a variable creates a symbol of the same name and points it the the respective object in the ...
0
votes
3answers
19 views
How to convert user input into MySQL date format?
In my Ruby on Rails 4 app I need to convert user entered dates like...
25.07.2014
25/07/2014
...possibly even...
25-07-2014
...into a the default Date format that can be stored in MySQL ...
0
votes
1answer
49 views
Regex for optional quotes, array of hashes and empty string
I want to write a regular expression for accepting the following types in ruby -
1) "value1"
2) 'value2'
3) "" or ''
4) [{:this => :that, "that" => :this}, {'foo' => :bar}, {:bar => 'foo'}]
So ...
0
votes
1answer
23 views
Traversal of binary tree in ruby
I'm writing a recursive toString method in a binary tree class in Ruby. The algorithm uses two methods as shown below. I have a local variable called result, which is to hold the concatenation of all ...
0
votes
3answers
32 views
Go to next lines when reading a file in Ruby
I'm reading a file line by line.
When I find a specific string I would like to analyze the following lines until a specific character is present.
In concrete. This is my input file:
blabbal
...
-1
votes
0answers
20 views
How do I convert a spreadsheet “letternamed” column coordinate to an integer?
In spreadsheets I have cells named like "F14" or "AK5". I have the first part, the column coordinate, in a variable and I want to convert it to a 0-based integer column index.
How do I do it, ...
1
vote
2answers
30 views
How can I convert a BigDecimal to a 2-decimal-place string?
I want to convert a BigDecimal object to a currency value to two decimal places. I don't want any rounding. How can I do it?
None of the following approaches worked:
v = BigDecimal("7.1762")
w = ...
0
votes
3answers
37 views
Rails string parse for forms
How do I parse out a string in rails? I have my form for submitting a height. Example: 5'9 I want the comma parsed and the 59 saved within the database
0
votes
3answers
28 views
ruby object expected, got String
I'm trying to create a song with associated tags, but my create method throws an error:
Tag(#70267554396440) expected, got String(#70267493763880)
My Songs#create:
def create
tags = ...
1
vote
3answers
46 views
Split string into chunks (of different size) without breaking words
I am trying to create a method that, given a string, returns three strings: title, description1, description2
This is a related question I found: Split a string into chunks of specified size without ...
0
votes
2answers
15 views
Ruby HTML attributes to Hash or Array
type="checkbox" name="prdCdList" value="102001174" class="bnone" newfl="Y" cpnfl="N" catcpnfl="N" eventfl="N" catcd1="102000" catcd2="102001" prdimgl="/upload/product/320_1405497216907.jpg" ...
0
votes
1answer
35 views
Ruby string prepend '\' character
why ruby is prepends '\' character while I am trying to run below code. It is happening with only '#$'
It is happening with all ruby version.
puts '#$' => '\#$'
or
'#$' => '\#$'
or
...
0
votes
1answer
17 views
Convert string to datetime format ex 2010-02-11T14:54:27.000Z
I have a string which I believe is in ISO8601 format, example 2010-02-11T14:54:27.000Z. How can I convert this string to the type datetime so I can save it?
I think rails requires you to also save ...
1
vote
2answers
34 views
Object vs String cloning and == operator
I don't understand why clone produces an Object that does not return true using the == operator, but clone on String does. Input appreciated.
obj = Object.new
copy = obj.clone
obj == copy # => ...
1
vote
1answer
38 views
ruby regexp find the pattern more than once
value = "Men - $30, Women - $20"
# value = "Men - 0"
# value = "free"
data = /\$(\d*)/.match value
Currently:
data.to_a
=> ["$30", "30"]
I want:
data.to_a
=> ["$30", "30", "$20", "20]
...
0
votes
3answers
46 views
How to check in ruby if an string contains any of an array of strings
I have an array of strings a, and I want to check if another long string b contains any of the strings in the array
a = ['key','words','to', 'check']
b = "this is a long string"
What different ...
-1
votes
2answers
26 views
Parse file, find a string and store next values
I need to parse a file according to different rules.
The file contains several lines.
I go through the file line by line. When I find a specific string, I have to store the data present in the next ...
0
votes
0answers
45 views
How to convert String to byte[] in ruby [duplicate]
I'm trying to use some function, that requested for byte[] in ruby. However, I cannot obtain bytes from string
When I using "foo" as function argument, error message was
NameError: no constructorfor ...
-1
votes
2answers
25 views
Split line with specific string in Ruby
I have to take specific data from a file. I need to consider only lines that contains a specific string ("--TEST:") and takes the remaining characters
My file contains several lines like:
# = = = = ...
-3
votes
1answer
44 views
How to get the maximum-character-matching string from an array of strings
I have an array of strings, which is a list of correct standard-disease names. I have another array of strings that is also a list of diseases with some variation in spelling; sometimes they are ...
1
vote
4answers
76 views
Replacing letters in a string in Ruby
I want to replace letters in a string using the following mapping:
letters = ("a".."z").to_a.zip(("b".."z").to_a.push("a"))
[["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"], ["e", "f"], ["f", "g"], ...
2
votes
4answers
53 views
How to join some (not all) array elements based on condition in ruby?
Say I have an array of strings like this:
array = ["foo", "(bar)", "baaz", "quux", "herp", "(derp)"]
And I need to join items starting with "(" with the previous item to get output like this:
...
0
votes
7answers
102 views
Find if all letters in a string are unique
I need to know if all letters in a string are unique. For a string to be unique, a letter can only appear once. If all letters in a string are distinct, the string is unique. If one letter appears ...
0
votes
1answer
28 views
string has trailing whitespaces that aren't white spaces? (i.e. strip doesn't get rid of it)
I have the following string I got from parsing some html:
"this is my string "
If I use .strip or .rstrip the string remains the same.
However if I literally type the string "this is my string " ...
1
vote
3answers
35 views
How to write an in-place method for strings
I want to write a method to trim characters from the end of a string. This is simple enough to do:
class String
def trim(amount)
self[0..-(amount+1)]
end
end
my_string = "Hello"
my_string = ...
0
votes
2answers
25 views
How can we set a variable after the user chooses from a HighLine menu in Ruby?
The HighLine documentation shows that we can display a string after the user selects an option from the list, like follows:
choose do |menu|
menu.prompt = "Please choose your favorite programming ...
1
vote
2answers
54 views
Using %w with runtime input
I'm trying to use %w with a string input at run time. When I run this program:
a = gets.chomp
puts %w(a).count
with the input 'hi how are you', the output is 1. %w(a).count doesn't replace a with ...
0
votes
3answers
57 views
Ruby - Converting Strings to CamelCase
I'm working on an exercise from Codewars. The exercise is to convert a string into camel case. For example, if I had
the-stealth-warrior
I need to convert it to
theStealthWarrior
Here is my code
...
0
votes
3answers
38 views
Method in a loop not looping - Ruby
def playmany
puts "Please enter in how many games you want to play"
games_to_play = gets.chomp.to_i
games_to_play.times do
puts "\n"
rules
end
end
(Absolute ruby noob ...
0
votes
1answer
21 views
Convert digest strings into byte strings
Suppose I hash a filename as such
require 'digest'
hashed = Digest::SHA256.digest("test")
path = "/myFile/%s" %hashed
p path
This would give me a path equal to
...
0
votes
1answer
30 views
JSON decoding by Oj converts a string into other object
I am using the Oj gem to decode JSON strings into Ruby objects. When I have an encoded string as in the example below, it is decoded as intended:
require "oj"
Oj.load("\"a\"") # => "a"
but when ...
0
votes
2answers
39 views
Ruby converting an array to a hash
I have the following string and I would like to convert it to a hash printing the below result
string = "Cow, Bill, Phone, Flour"
hash = string.split(",")
>> {:animal => "Cow", :person: ...
0
votes
3answers
41 views
Pull array from inside a Ruby string
I have a string that contains an array;
"["item1","item2","item3"]"
Is there a slick Ruby way to convert it to this;
["item1","item2","item3"]
0
votes
1answer
47 views
Extracing words from a string?
I have a string "---\n- bb\n- j2me\n". I want to extract the words and save it into an array. Like ['bb','j2me']. I tried the below but its not working.
"---\n- bb\n- j2me\n".split("\n")
If you ...
0
votes
4answers
51 views
Converting a multi line string to an array in Ruby using line breaks as delimiters
I would like to turn this string
"P07091 MMCNEFFEG
P06870 IVGGWECEQHS
SP0A8M0 VVPVADVLQGR
P01019 VIHNESTCEQ"
into an array that looks like in ruby.
["P07091 MMCNEFFEG", "P06870 IVGGWECEQHS", ...
0
votes
3answers
61 views
Trying to split a string and apply alternate suffixes
I have a string:
str='SA201406005/007/009/010'
how to get this result?
['SA201406005','SA201406007','SA201406009', 'SA201406010']
This code works:
arr=[]
arr=str.split(/\//)
...
1
vote
3answers
41 views
Replace in Ruby commas by different strings
I have a string called "example", like this:
192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812
192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807
and I would like to obtain this output:
...
1
vote
3answers
52 views
Ruby converting letters in string to letters 13 places further in the alphabet
I'm trying to solve a problem where when given a string I convert each letter 13 places further in the alphabet. For example
a => n
b => o
c => p
Basically every letter in the string is ...
1
vote
3answers
59 views
Using gsub or sub to replace one character
I want to replace only the third character of a string by its upper case counterpart. Using gsub or sub is causing the following error:
"Hiiiii".sub!(string[2,1],string[2,1].upcase!) # => hIiiii
...
0
votes
4answers
69 views
Strings to Integers
Say you have the string "Hi". How do you get a value of 8, 9 ("H" is the 8th letter of the alphabet, and "i" is the 9th letter). Then say, add 1 to those integers and make it 9, 10 which can then be ...
-4
votes
2answers
62 views
Convert Array to String and back to Array
I have built a presenter class whose only job is to convert a given array into a string. I am test driving the solution, so I started with "[nil, nil, nil]" but each nil will eventually be replaced ...
1
vote
2answers
56 views
Concat double quotes with String in Ruby
I need a string with double quotes
tried
expectedValue = "/""+expectedValue+"/""
but did not worked -
Throws below error -
NoMethodError: undefined method `/' for "/+expectedValue+":String
...
0
votes
1answer
39 views
Ruby - Editing URLs
I used the following question, Remove "www", "http://" from string, to remove the HTTP and HTTPS from my url. Now I want to remove the path on the end of the url as well and can't ...
0
votes
2answers
40 views
Ruby: searching for string with partial dynamic contents (Interpolated REGEX??)
How in ruby is it possible to search text for a string that can have varying contents e.g.
text.include?("match here[generic//regex]and here").should == true
the goal is for the regex to match ...
1
vote
2answers
82 views
Find all permutations of a 4-letter string that starts with “j” and includes “w”?
I need to find all possible 4-letter combinations that start with "j" and have "w". I can imagine doing it with nested loops, but am sure there is a better way to achieve it with array combination or ...