create an instance for each element of an array ruby [closed] - ruby

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

Related

Ruby how to get file from public dir? [closed]

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 2 years ago.
Improve this question
def get_xml
path = "ddd-66252.pdf" // in public
way = File.basename(path)
diff = File.read(path)
render :xml => diff
end
How i can get file from path and need to look like file
Assuming you are using Ruby on Rails because you mention a public folder and have a render method in your code. In Ruby on Rails you can use send_file in your controller like this to send files to the browser:
def send_pdf
send_file(
Rails.root.join('public', 'ddd-66252.pdf'),
filename: 'ddd-66252.pdf',
type: 'application/pdf'
)
end

How to retrieve value of XML element using a Nokogiri SAX Parser? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How does one access the text value of a nested element using a Nokogiri SAX parser?
require 'nokogiri'
xml = <<-eos
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/example-sitemap.xml</loc>
</sitemap>
</sitemapindex>
eos
class MySAXDoc < Nokogiri::XML::SAX::Document
def start_element name, attrs=[]
if name == "sitemap"
# from here, how can one retrieve the value of the child element, `loc`?
end
end
end
sax_parser = Nokogiri::XML::SAX::Parser.new(MySAXDoc.new)
sax_parser.parse(xml)
You can't read ahead, so you must keep track of the current context within the file yourself. Something along these lines should do the trick:
def start_element(name, attrs = [])
#element = name
if name == 'sitemap'
#sitemap = true
end
end
def end_element(name)
#element = nil
if name == 'sitemap'
#sitemap = false
end
end
def characters(string)
if #element == 'loc' && #sitemap
# The local variable 'string' holds the text contents of the <loc> tag
# so do something with it here
puts string
end
end
How this works: When a new element is started it checks to see if it is a and if so sets a #sitemap variable. On the next iteration when the element is it checks #sitemap to see if it is within a sitemap and does something with its contents.

Passing replacement string as parameter [closed]

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"

How do you change a value in a hash using a variable in ruby? [closed]

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.

How do I return a value from inside a loop? [closed]

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 read an XML file and store the structure into an array of objects. Here is my code:
class Bike
attr_accessor :id, :color, :name
def initialize(id, color, name)
#id = id
#color = color
#name = name
end
end
---x---snip---x---
rules.root.each_element do |node1|
case node1.name
when "Bike"
bike = Bike.new(node1.attributes['id'], node1.attributes['color'], { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name"})
bikes.push bike
end
end
However, the last element is not fetching the value alone. It is fetching the whole tag. Is there a better way to do it?
Your code block { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name" }
doesn't seem to make sense inside the new parameter list.
Where does bike_elem come from? This is not valid Ruby in this context.
It's hard to give an answer here without knowing what your XML looks like.
But I would recommend using a XML library like Nokogiri, libxml, and then parse out
the name before you do the new. Try using XPath.
rules.root.each_element do |node1|
case node1.name
when "Bike"
name = node1.attributes[....]
bike = Bike.new(node1.attributes['id'], node1.attributes['color'],name )
bikes.push bike
end
end

Resources