How to modify user input? - ruby-on-rails

I wrote an little demo code, to show you my problem:
puts "Enter your feeling"
a = gets.chomp
#feel = "good"
puts a
SO when it comes to the input, i type in:
Actually i fell very #{#feel}
Then i hope to get this output:
Actually i fell very good
But instead i get this output:
Actually i fell very #{#feel}
What did i make wrong?

It honestly isn't too safe to interpolate arbitrary strings from the user. Basically it's the same as using Kernel#eval (which will work in your case) ... the user can put whatever they want and it is extremely easy to abuse and compromise your system. You could technically do a little by simply filtering out quotation marks in what they type, but honestly it's a bad choice.
What you can do is do a string replace for #{#feel} with #feel, if #feel is the only thing you are going to allow them to interpolate:
puts a.gsub('#{#feel}',#feel)

"#{#feel}"
may be this what you need I feel.
and try to follow naming conventions.

instead of
puts a
it should be
puts "Actually i feel very " + #feel

Simple answer is you can't.
Interpolation happens at run time, so even if you could get this to work, #feel would be nil at the time you input it.
You would be better doing something like:
puts "Enter your feeling"
a = gets.chomp
#feel = "good"
puts a.gsub("##feel##",#feel)
Then asked how you feel enter:
Actually I feel very ##feel##

Related

How to get around an illegal octal digit in a url?

I have a method in my rails app that saves the image from an og:image tag url.
def photo_from_url(url)
if !Nokogiri::HTML(open(url)).css("meta[property='og:image']").blank?
photo_url = Nokogiri::HTML(open(url)).css("meta[property='og:image']").first.attributes["content"]
self.photo = URI.parse(photo_url)
self.save
end
end
This works in most cases, except when the image url happens to include a number that start with 0, such as http://ad009cdnb.website.com/rest-of-url
In these cases, I get an illegal octal digit error.
How can I prevent the method from thinking that any numbers starting with zero are base-8?
This is not a answer. Sorry for use the system wrongly. Can't find out how to put a string with HTTP without change it in a link as comment.
URI.parse works fine:
URI.parse("http://ad009cdnb.website.com/rest-of-url").to_s
=> "http://ad009cdnb.website.com/rest-of-url"
as everyone said, we really need a stacktrace or something to help you. And most likely you will want to save the URL as a string, not as an object on the database.
Hi guys I posted this question, but I can no longer reproduce the question.
As this is no longer an issue, I think the best course of action is to close this question. However, I see that some people have been voted up for their comments (They make good points like saving as a string instead of an object). Will they be discredited if the questions is closed?
I'm grateful for everyone's input, and don't want to upset anyone. What is SO protocol for a situation like this?

Capitalization API

Is there any good API or service that handles capitalization well? It should be able to handle input like "i need help fixing my iphone asap" with a desired output of "I Need Help Fixing My iPhone ASAP".
Edit: This is in conjunction with titleize. Titleize doesn't handle words like "iPhone" and acronyms. I'm currently getting user input like "ceo" and titleize returns "Ceo", when I'd like "CEO". I'd prefer not to write a list of special capitalizations, especially if there is a good alternative.
Another alternative would be a library of words and the correct capitalization.
Have a look at #titleize in ActiveSupport::Inflector
"man from the boondocks".titleize # => "Man From The Boondocks"
"x-men: the last stand".titleize # => "X Men: The Last Stand"
"TheManWithoutAPast".titleize # => "The Man Without A Past"
"raiders_of_the_lost_ark".titleize # => "Raiders Of The Lost Ark"
Cut and paste from http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-titleize

Is there a way to provide better rspec test descriptions

I have been using rspec for a little while and recently switched style from
it "should do something cool" do
#something.should work
end
to the more concise
subject(#something)
it {should work}
Whilst I much prefer the more concise style when writing the tests and for viewing them in code, I miss being able to specify the description for each test, particularly since the equality messages just display the values that are being tested. So in the example above, assuming the test passes, with the first style, I would get a message saying 'it should do something cool', whereas the second will just say that it has worked.
Does anyone know of a way to do this? Cheers
I think you got the wrong concept of what the specify/subject blocks do. They are not meant to be a complete replacement for the more verbose syntax you switched from, but should be used when there is no need for a desciption.
So if you want a desciption, just use
it "should do something cool" do
#something.should work
end
Also, I personally don't think that the specify/subject is more concise. For me it's a step away from the more DSL-like way specifications created with rspec read, but that might be a matter of personal preference.

Am I abusing "rescue" for nil checks?

I use rescue for everything, not just for "rescuing" exceptions. I mean, I just love the way it spares me verification and double checking data.
By example, lets say I have a model Item what may or may not have a User. Then, when I want to get the owner's name of the item I write:
item.user.name rescue ""
instead of something like
item.user.nil? ? "" : item.user.name
It makes the same think, since nil.name trigger an exception that I rescue with "", but I'm not pretty sure this is good practice. It makes what I want to, and it makes it with less code, but... I don't know, all that rescue words here and there makes me feel insecure.
Is it a bad practice or is it valid abusing of the rescue keyword?
I think you are abusing rescue a bit, though in Rails, there is a specific method for these issues: try. Documentation
In your case, item.user.try(:name) might be a nicer approach.
I would say this is not really a good habit to get into. I've never really used this feature in Ruby since it feels like I'm just hiding error cases. It's also worth noting that you're rescuing any and all exceptions without specifying any type of expected error. It's just always looked like something which is going to make debugging down the road harder than it needs to be, although, like I've said, I've never bothered to use it myself.
Like in most other languages, making the check yourself will run faster than using rescue.
As an alternative to your rescue abuse, check out the andand gem. It's similiar to the try another posted suggested, but nicer. andand lets you say:
item.user.andand.name
The expression will be nil if item.user is nil.

convert HASH into ARRAY

After saving some values into the database, I am
finding it difficult to print them out. Though I have been able to
pull the data out of the database, the output is like the following:
#vars={:object=>"46789620999001", :source_id=>1, :comment=>"["This is
my first commenttoday and tommorrow", "This is my first commenttoday
and tommorrow", "This is my first commenttoday and tommorrow", "This
is my first commenttoday and tommorrow", "This is my first comment",
"This is my first comment", "its nice!!!", "Many people do not take
this serious. In this life we have a big role to play in making
ourselves what we would be. It is only God that can help us to live
that life which we have planned, so we can only pray to Him who is the
all and all in our life to help
us."]", :title=>"", :content=>"<div>Life is beautiful. In this life,
whatever you see is what you will use to make out your way. People
around you can help you in many things and ways but can never live
your life for you. It is left for you to live your life, make and take
decisions that you think will help you in living out your dream life.
I believe everybody has a dream life he would like to live. Whatever
decisions one take today will go a long way in determining the kind of
life the one will live in future.<br />Take this as an advise.Bye </
div><div class="blogger-post-footer"><img width='1' height='1'
src='https://blogger.googleusercontent.com/tracker/
6876835757625487750-2447909010390406819?l=godwinagada.blogspot.com'
alt='' /></div>", :author=>"["Godwin",
"ken"]", :category=>"Reality", :post_id=>"", :date=>"2010-06-04", :FileName=>"first"}
>]
please can someone help out in referring to each of the data in this
output eg.
#output.each { |g|
puts g.FileName
puts g.post_id
}
etc
Don't you want:
#vars[:FileName]
#vars[:post_id]
You have a hash, which contains a set of keys with each key pointing to a value. There are several ways you can deal with them:
If you want to just view it to debug it. Load pretty print (require 'pp') and pretty print it (pp #vars). An even better choice is the Awesome Print gem.
If you output the value of each pair, just iterate with each passing a block for your action:
#vars.each do |key, value|
puts "#{key} => #{value}
end
Try pp, from the standard library.
require 'pp'
pp #vars
There is another alternative called awesome_print, you can dl the gem from http://rubygems.org/gems/awesome_print that would look like this
require 'rubygems'
require 'ap'
ap #vars
Either of these should print the hash in a format that is easier to read.

Resources