How to replace special text in a string in Ruby? [closed] - ruby

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 7 years ago.
Improve this question
There is one string like this: 'xxxxxxx#code#xxxxxxxx', I want to replace the code.

Use regex and string.gsub()
2.0.0-p643 :004 > "xxxxxxx#code#xxxxxxxx".gsub(/\#code#/i, "#NEWCODE#")
=> "xxxxxxx#NEWCODE#xxxxxxxx"

Related

Have to return the key while I have specific value in the hash [closed]

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 3 years ago.
Improve this question
I have a hash like the one shown below
profile={"acakxqcnwbhtfcppyilk"=>"unlocked", "achurktnaavqqnwtfvvt"=>"locked", "chrofmjydwzcbswhpste"=>"locked", "ChromeProfile"=>"unlocked", "clcqozsfdblntkwlcheo"=>"unlocked", "gpqhioztlmkoitjqxerm"=>"unlocked", "hododpaxflyzgpwortjl"=>"unlocked", "hyqnjrpttwclueqwttdw"=>"unlocked", "jtdeyzcxdpgblxmpldtx"=>"unlocked", "kifxvxmbifkicmapedir"=>"unlocked", "lucjkeeqzynhjurnpewl"=>"unlocked", "lyccchkgyscmljcvkvcj"=>"unlocked", "nmqhlowcqnwmwbxijwry"=>"unlocked", "nseucpicwbcyviargwjt"=>"unlocked", "osecuzrbvodwgkdwovjd"=>"unlocked", "pqhlxugxqppfybxdkemr"=>"unlocked", "qgoaryzyriohpwzbprgg"=>"unlocked", "rwtlttvtbrmziyuimgad"=>"unlocked", "sxkcvnlsgqauwcbkmjcy"=>"unlocked", "uyfvlzyllwimhklmmmns"=>"unlocked", "vgvobxhpflhappnajizs"=>"unlocked", "vlbbphwoyweyguhcmdwv"=>"unlocked", "vrsjncafxunswclescvu"=>"unlocked", "wxsninefjvtrxvntgkni"=>"unlocked", "xdqndtyyxctkovyfsldi"=>"unlocked", "ycvguesevlbopicmxfbc"=>"unlocked"}
I have to return the key which has the value 'locked', If there are more than one 'locked' is present, then the first one has to be returned.
Is there any specific method is available to accomplish task?
Try the key() method
profile.key('locked')
Read more here

Negative string value in hash 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 5 years ago.
Improve this question
I'm trying to use a negative value in a string which is in a hash.
{"amount"=>"-50.01", "currency"=>"CAD"}
If I write transaction.amount I get 0.5001E2, if I use to_f I get 50.01.
I'm trying to extract the -50 value.
Thank you for your help!
Try
transaction['amount']
For example
a = {"amount"=>"-50.01", "currency"=>"CAD"}
Then
a['amount']
returns '-50.01'

is it possible have ruby read 12:34 as a integer? [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 6 years ago.
Improve this question
I'm working with a script that only takes integer values. I'm reading from a CSV with a timestamp example of 12:34. Is there a way I get ruby to read it as an integer?
Not sure if you are dealing with a Time object. If so:
str = Time.now.to_s[/\d\d:\d\d:\d\d/]
=> "19:04:53"
str.gsub(":", "").to_i
=> 190453
If it's a string just start with the sub:
"12:34".sub(":", "").to_i
=> 1234

Is it possible to pass parameter as argument of a method like this 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 9 years ago.
Improve this question
Is it possible to pass parameter to a method like this:
variable = my_method(:parameter)
No quotes, no nothing.Just -> :parameter .
Yes, you can pass a symbol to a method. Example:
puts('hello')
or
puts(:hello)

Magento - is it acceptable to create a collection from within a template [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Just as the title implies, is it acceptable to create and use a collection from within a magento template file?
I would say no, use blocks for this

Resources