[Solved] Generate string containing escaped interpolation

You have not the ‘\’ in the string, it is added by the inspect: if you puts the string you will realize it: asd = ‘<%= asd %>’.gsub(/<%=(.*)%-?>/, “\#{\\1}”) #=> “\#{ asd }” p asd #=> “\#{ asd }” <- this is `asd.inspect`, which is returned by `p` “\#{ asd }” <- this is `asd.inspect`, … Read more

[Solved] For some reason, this Ruby script is not working

There are multiple issues with your code: You have syntax errors. You need end after each of your if and else statements unlike python. From your code it looks like you are looking for the if-elsif statement and not the multiple if statements because the else statement will be of the last if. You need … Read more

[Solved] hashes — Seem like mutant potatoes [closed]

There’s a wealth of information out there, http://ruby-doc.org/core-2.3.0/Hash.html for example is pretty clear on what hashes are. You need to get your head around some of the basics, there are plenty of tutorials out there, https://www.railstutorial.org/ is one that comes up often, http://guides.rubyonrails.org/getting_started.html is the most obvious place to start. 2 solved hashes — Seem … Read more

[Solved] Syntax Error in Ruby. unexpected keyword_end, expecting end-of-inputs [closed]

You can’t define a module using module Auth(): irb(main):001:0> module Auth() irb(main):002:1> end SyntaxError: (irb):1: syntax error, unexpected ‘\n’, expecting :: or ‘[‘ or ‘.’ and irb(main):001:0> module Auth() irb(main):002:1> def login(id) irb(main):003:2> end irb(main):004:1> end SyntaxError: (irb):1: syntax error, unexpected ‘\n’, expecting :: or ‘[‘ or ‘.’ (irb):4: syntax error, unexpected keyword_end, expecting end-of-input … Read more

[Solved] Can’t figure out why match result is nil [closed]

Regex is the wrong tool for handling HTML (or XML) 99.9% of the time. Instead, use a parser, like Nokogiri: require ‘nokogiri’ html=”<img src=”https://filin.mail.ru/pic?width=90&amp;height=90&amp;email=multicc%40multicc.mail.ru&amp;version=4&amp;build=7″ style=””>” doc = Nokogiri::HTML(html) url = doc.at(‘img’)[‘src’] # => “https://filin.mail.ru/pic?width=90&height=90&email=multicc%40multicc.mail.ru&version=4&build=7” doc.at(‘img’)[‘style’] # => “” Once you’ve retrieved the data you want, such as the src, use another “right” tool, such as … Read more

[Solved] Converting a code from Python to Ruby [closed]

Your Python code outputs: a=1&b=2&aaa=bbb In Ruby we might do this as: require ‘cgi’ parameters = {a: 1, b: 2} encoded = parameters.merge(Hash[*[‘aaa’, ‘bbb’]]) .map { |key, value| “#{CGI.escape key.to_s}=#{CGI.escape value.to_s}” } .join(‘&’) p encoded Which outputs the same: a=1&b=2&aaa=bbb It’s longer than the Python code, but IMHO also slightly more readable … You could … Read more

[Solved] Understanding Ruby for a C# developer [closed]

Are there files like DLLS formed? No. Ruby is an interpreted language much like JavaScript, Python, and PHP. If there is not a main() defined, what is the entrypoint for a program? Well, what do you define as an entry point? If you mean running a particular file, then anything not wrapped in a class … Read more

[Solved] ruby, undefined method `text’ for nil:NilClass

You have several options price_element = result.search(‘span.result-price’)[0] price = price_element ? price_element.text : ‘$0000’ or price = (result.search(‘span.result-price’)[0].text rescue ‘$0000’) from Ruby 2.3.0 you can take advantage of safe navigation operator price = result.search(‘span.result-price’)[0]&.text || ‘$0000′ 10 solved ruby, undefined method `text’ for nil:NilClass

[Solved] Get value from Ruby hash

Hash is within an array so use this p FIXED_COUNTRY_TO_PHONE.map{|x| x[:country]} output [“FI”, “SE”] If you want to take the first country then p FIXED_COUNTRY_TO_PHONE.first[:country] If you want to take the last country then p FIXED_COUNTRY_TO_PHONE.last[:country] Getting the country code according to country p FIXED_COUNTRY_TO_PHONE.detect{|x| x[:country].eql?’FI’}[:customer_phone] 12 solved Get value from Ruby hash

[Solved] rails passing in id parameter ambiguous

Extract from Rails Routing from the Outside In 2.10.1 Adding Member Routes To add a member route, just add a member block into the resource block: resources :photos do member do get ‘preview’ end end This will recognize /photos/1/preview with GET, and route to the preview action of PhotosController, with the resource id value passed … Read more