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 a code:
require 'pp'
def unquote_string(string)
if (string.is_a?(String))
string.gsub(/\\/,'')
else
string
end
end
def filter_array_with_substitution_and_replacement(array,options={})
pp options
return array unless %w(filterRegex substitudeRegex replacementExpression).any? {|key| options.has_key? key}
puts "I will substitude!"
filterRegex = options["filterRegex"]
substitudeRegex = options["substitudeRegex"]
replacementExpression = options["replacementExpression"]
pp "I have: #{replacementExpression}"
array.select{|object|
object =~ filterRegex
}.map!{|object|
object.sub!(substitudeRegex,unquote_string(replacementExpression))
}
end
def sixth_function
array = %w(onetwo onethree onefour onesix none any other)
filterRegex = /one/
substitudeRegex = /(one)(\w+)/
replacementExpression = '/#{$2}.|.#{$1}/'
options = {
"filterRegex" => filterRegex,
"substitudeRegex" => substitudeRegex,
"replacementExpression" => replacementExpression
}
filter_array_with_substitution_and_replacement(array,options)
pp array
end
def MainWork
sixth_function()
end
MainWork()
Output:
{"filterRegex"=>/one/,
"substitudeRegex"=>/(one)(\w+)/,
"replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
I will substitude!
"I have: /\#{$2}.|.\#{$1}/"
smb192168164:Scripts mac$ ruby rubyFirst.rb
{"filterRegex"=>/one/,
"substitudeRegex"=>/(one)(\w+)/,
"replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
I will substitude!
"I have: /\#{$2}.|.\#{$1}/"
["/\#{$2}.|.\#{$1}/",
"/\#{$2}.|.\#{$1}/",
"/\#{$2}.|.\#{$1}/",
"/\#{$2}.|.\#{$1}/",
"none",
"any",
"other"]
It is not correct, because string with replacement have metacharacters quoted. how to correct unquote this string replacementExpression?
Desired output for array after replacement:
["two.|.one/",
"three.|.one/",
"four.|.one/",
"six.|.one/",
"none",
"any",
"other"]
Forget unquote_string - you simply want your replacementExpression to be '\2.|.\1':
"onetwo".sub(/(one)(\w+)/, '\2.|.\1')
#=> "two.|.one"
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 8 years ago.
Improve this question
I am trying to create an instance of a class that each includes an element from the class's array. The array is like this
class CorporatePanel < ActiveRecord::Base
TEXT_ONLY = 'text only'
IMAGE_LEFT = 'image left'
IMAGE_RIGHT = 'image right'
IMAGE_ONLY = 'image only'
VIDEO = 'video'
PANEL_TYPE = [TEXT_ONLY, IMAGE_LEFT, IMAGE_RIGHT, IMAGE_ONLY, VIDEO]
So for each panel type i want an instance of the class corporate panel.
#corp_page = CorporatePage.create!(title: 'Home', static_descriptor: 'Home', workflow_state: 'draft')
#corp_panel = CorporatePanel::PANEL_TYPE
if #corp_page.title == 'Home'
puts "PAGE CREATED"
(#corp_panel.count).times do
corp_panel = CorporatePanel.new
#corp_panel.each do |x|
corp_panel.panel_type = x
end
if corp_panel.save
puts "#{corp_panel.title} created"
else
puts "#{corp_panel.title} not created"
puts corp_panel.errors.inspect
end
end
else
puts "Corp Page not saved"
puts #corp_page.inspect
end
So on every iteration through corporate panel it uses a different panel type. At the moment it creates five of each panel!
Thanks in Advance folks
Looking at the array documentation i found the method 'pop', to remove the last element of every array, seeing as this constant is an array, it worked.
corp_panel.panel_type = CorporatePanel::PANEL_TYPE.pop
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 have the URL http://localhost:3000/en/invest_offers?utf8=blahblahblah. How can I get ?utf8=blahblahblah part of the URL? Thanks!
Do as below using URI::Generic.query:
require 'uri'
URI("http://localhost:3000/en/invest_offers?utf8=blahblahblah").query
# => "utf8=blahblahblah"
Use URI::parse, URI::Generic#query:
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
URI.parse(url)
# => #<URI::HTTP:0x0000000213aae0 URL:http://localhost:3000/en/invest_offers?utf8=blahblahblah>
URI.parse(url).query
# => "utf8=blahblahblah"
Do the following:--
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
parsed_url = URI.parse(url)
if you just want to get string query part like "utf8=blahblahblah"
you should do
parsed_url.query
if you want get "blahblahblah",
you should just do
params["utf8"]
or else
p = CGI.parse(parsed_url.query)
# p is now {"utf8"=>["blahblahblah"]}
p["utf8"].first
#=> "blahblahblah"
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 am trying to make a program to guess the job the user is thinking of, I have become stuck on a point to do with the updating of the database after the program has made an incorrect guess.
I use a variable to store the stage at which the user is, and am trying to use it to replace the value that it refers to. I've simplified the program and pasted it below:
#question_database = {'Does it need training?' => 'Mechanic' , 'Is it to do with sports?' => {'Is it to do with teaching?' => 'P.E. Teacher', 'Is it competitive?' => 'Athlete'} }
#question_stage = #question_database['Does it need training?']
#job = 'Mechanic'
puts "What is a question that could identify a #{#job}?"
primary_answer = #job
primary_question = gets.chomp
puts 'Which job were you thinking of?'
secondary_answer = gets.chomp
puts 'What is a question that could identify it?'
secondary_question = gets.chomp
#question_stage={ primary_question => primary_answer, secondary_question => secondary_answer}
It doesn't seem to do anything at all to the database.
Does anyone know what I should do to fix this?
Thanks, Ben
When you assign a new value to #question_stage you don't change the value of #question_database['Does it need training?']
You might want to write your code like this:
#question_database = {'Does it need training?' => 'Mechanic' ,
'Is it to do with sports?' =>
{'Is it to do with teaching?' => 'P.E. Teacher',
'Is it competitive?' => 'Athlete'} }
#job = 'Mechanic'
puts "What is a question that could identify a #{#job}?"
primary_answer = #job
primary_question = gets.chomp
puts 'Which job were you thinking of?'
secondary_answer = gets.chomp
puts 'What is a question that could identify it?'
secondary_question = gets.chomp
#question_database['Does it need training?'] = { primary_question => primary_answer,
secondary_question => secondary answer }
That is, of course, if I understand what you were trying to do, anyway to change the #question_database you need to assign to one of its keys, or change one of its values.
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'm beginner in programming, I changed my code a little bit so that I can execute some def from the command line. After that change I got an error, but first my code:
require 'faraday'
#conn = Faraday.new 'https://zombost.de:8673/rest', :ssl => {:verify => false}
#uid = '8978'
def certificate
res = conn.get do |request|
request.url "acc/#{#uid}/cere"
end
end
def suchen(input)
#suche = input
#res = #conn.get do |request|
request.url "acc/?search=#{#suche}"
end
end
puts #res.body
Then I wrote into the console:
ruby prog.rb suchen(jumbo)
So, somehow i get the error:
Undefined method body for Nilclass
You're not invoking either of your methods, so #res is never assigned to.
#res evaluates to nil, so you're invoking nil.body.
RE: Your update:
ruby prog.rb suchen(jumbo)
That isn't how you invoke a method. You have to call it from within the source file. All you're doing is passing an argument to your script, which will be a simple string available in the ARGV array.
RE: Your comment:
It should go without saying that the solution is to actually invoke your method.
You can call the methods from the command line. Ruby has a function eval which evaluates a string. You can eval the command line argument strings.
Here's how. Change the line puts #res.body to
str = ARGV[1]
eval(ARGV[0])
puts #res.body
then run your program like so
$ ruby prog.rb suchen\(str\) jumbo
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 have some model in which I have the following code:
appraisal_detail.each_value { |detail|
# some code...
}
It is raising NoMethodError with the message, "undefined method 'each_value'". Am I doing anything wrong? Any suggestions would be welcome.
Here is the full content of the method raising the error:
def self.update_appraisal_details(appraisal, appraisal_detail)
status = true
appraisal_detail_ids = appraisal.employee_appraisal_detail_ids
appraisal_detail.each_value { |detail|
appraisal_detail = find_by_employee_appraisal_id_and_kra_list_id(appraisal.id, detail[:kra_list_id])
if appraisal_detail.nil? # For newly added KRA
new_detail = EmployeeAppraisalDetail.new(detail.merge({:employee_appraisal_id => appraisal.id}))
status = (new_detail.save && status)
elsif appraisal_detail.status == "Inactive"
status = (appraisal_detail.update_attributes(:status => "Active") && status)
appraisal_detail_ids.delete(appraisal_detail.id)
else
appraisal_detail_ids.delete(appraisal_detail.id)
end
}
end
You should pass Hash object as appraisal_detail.
>> {1=>2}.each_value {|x| p x}
2
It seems like you are passing non-Hash object.
>> [].each_value { |x| p x }
NoMethodError: undefined method `each_value' for []:Array
from (irb):1
from /usr/bin/irb:12:in `<main>'