Lambda returning different values [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 9 years ago.
Improve this question
I'm struggling with the following code:
I want a method to check if a string has content or not.
has_content = -> (a) { a!=nil && a.strip != ''}
c = ' '
has_content.call(c)
=> false
c.has_content
=> true
Why is the response different? Clearly I am lacking some Proc/lambdas knowledge.

I believe there is something missing in that code that is causing such behavior.
has_content is not defined for String, so unless you defined it before, it should raise an error
1.9.3p429 :002 > ''.has_content
NoMethodError: undefined method `has_content' for "":String
from (irb):2
from /Users/weppos/.rvm/rubies/ruby-1.9.3-p429/bin/irb:12:in `<main>'
As a side note, here's an alternative version of your code
has_content = ->(a) { !a.to_s.strip.empty? }
And here's an example
has_content.(nil)
# => false
has_content.('')
# => false
has_content.(' ')
# => false
has_content.('hello')
# => true

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

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"

Ruby: remove from the string [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 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"

Argument should be a vector [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to use the statsample library, but having issues with arrays/vectors.
b = [2,3,4,5,6,7].to_scale
# => #<TypeError: Argument should be a Vector>
Do you know why I might be getting this error?
EDIT 1
Something odd is going on in my environment....
$ irb
irb(main):001:0> require 'statsample'
=> true
irb(main):004:0> b = [2,3,4,5,6,7].to_scale
=> Vector(type:scale, n:6)[2,3,4,5,6,7]
exit
$ bundle exec irb
irb(main):001:0> b = [2,3,4,5,6,7].to_scale
NoMethodError: undefined method `to_scale' for [2, 3, 4, 5, 6, 7]:Array
from (irb):1
from /Users/brandon/.rbenv/versions/1.9.3-p484/bin/irb:12:in `<main>'
irb(main):002:0>
For some reason statsample is not being required when I use bundle exec. I have to manually require 'statsample in my code, even though gem 'statsample is in my Gemfile.
Any thoughts??
I don't see the issue:
irb(main):004:0> require 'statsample'
=> true
irb(main):004:0> b = [2,3,4,5,6,7].to_scale
=> Vector(type:scale, n:6)[2,3,4,5,6,7]
Please make sure that if you use the bundler, put into the Gemfile the following:
gem 'statsample'
And execute the bundle install.
According to the source code:
module Statsample::VectorShorthands
# Creates a new Statsample::Vector object
# Argument should be equal to Vector.new
def to_vector(*args)
Statsample::Vector.new(self,*args)
end
# Creates a new Statsample::Vector object of type :scale
def to_scale(*args)
Statsample::Vector.new(self, :scale, *args)
end
end
class Array
include Statsample::VectorShorthands
end
So here my guess is:
If it's just [Array].to_scale, it should have no problem at all. Unless you pass any argument to to_scale() which is not Vector type, because inside it's calling Statsample::Vector.new(self, :scale, *args), and it's saying "Argument should be equal to Vector.new".

each_value raising NoMethodError [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 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>'

Resources