convert HASH into ARRAY - ruby-on-rails

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.

Related

middleman blockquote ruby function

I am trying to create a method in ruby to handle block quotes from my slim blog posts. Right now, I have the following view helper:
def blockquote(content,author,source=nil,source_link=nil)
data = '
<blockquote>
<p>'+content.html_safe+'</p>
<footer>
<strong>'+author+'</strong>'
if source && source_link
data = data + '
<cite>
'+source+'
</cite>
'
end
data = data + '</footer></blockquote>'
return data
end
Which works for posts like..
= blockquote("When you grow up you tend to get told that the world is the way it is and you're life is just to live your life inside the world. Try not to bash into the walls too much. Try to have a nice family life, have fun, save a little money. That's a very limited life. Life can be much broader once you discover one simple fact: Everything around you that you call life was made up by people that were no smarter than you. And you can change it, you can influence it… Once you learn that, you'll never be the same again.","Steve Jobs, Apple Computers")
however, would there be a better way? I want to wrap the text in something like
- blockquote
When you grow up you tend to get told that the world is the way it is and you're life is just to live your life inside the world. Try not to bash into the walls too much. Try to have a nice family life, have fun, save a little money. That's a very limited life. Life can be much broader once you discover one simple fact: Everything around you that you call life was made up by people that were no smarter than you. And you can change it, you can influence it… Once you learn that, you'll never be the same again.
but I need a way to pass in the author, and option source + source link.
The current method also requires the quote to be on a single line, instead of multiple lines. Thoughts?
I would create a quote_template partial instead of coding html into a method like you did. Then just do something like this:
def blockquote(options={})
... #checks and business logic
render partial: 'quote_template', options
end
And you can initialize it like:
blockquote({
author: "Steve Jobs",
quote: "Some awesome quote...",
...
})
_quote_template.html.erb partial might look something like this:
<blockquote>
<p><%= options[:quote] %></p>
<footer>
<strong><%= options[:author] %></strong>
<% if options[:source] %>
<cite>
<%= options[:source] %>
</cite>
<% end %>
</footer>
</blockquote>
I created the following slim template in my partials directory:
blockquote
p
= locals[:quote]
footer>
strong
= locals[:author]
- if locals[:source]
cite
a[href="#{locals[:source_link]}"]
= locals[:source]
which can be called via middleman partials like:
= partial "components/blockquote", locals: {\
author: "Steve Jobs, Apple Computers",\
quote: "When you grow up you tend to get told that the world is the way it is and you're life is just to live your life inside the world. Try not to bash into the walls too much. Try to have a nice family life, have fun, save a little money. That's a very limited life. Life can be much broader once you discover one simple fact: Everything around you that you call life was made up by people that were no smarter than you. And you can change it, you can influence it… Once you learn that, you'll never be the same again."}

How to modify user input?

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##

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

Complex search screens in Rails 3

I need to implement some search functionality within a Rails application. Most of the stuff I have found is generally aimed at simple plain-text search. I am trying to implement something much more specific. The sort of functionality I am looking to create is this (from a C application):
http://andyc.ac/query.gif
The form just submits the data entered by the user. So I need to translate strings like "3..7" into SQL conditions for the where method e.g.
TestLine.where( "test_int >= ? and test_int <= ?", MinInt, MaxInt )
It seems like this is something that already exists somewhere. The exact format expected is not too important, as the users are not shared between the Rails and C applications. How would this be done?
FWIW the specific functionality you describe is actually supported directly. Well.. almost. From the docs:
A range may be used in the hash to use the SQL BETWEEN operator:
Student.where(:grade => 9..12)
Of course then it's a matter of translating the user's string input to a Range, which isn't very complex, e.g.:
def str_to_range str
str =~ /(\d+)\.\.(\d+)/
Range.new *$~.captures.map(&:to_i)
end
It would probably make the most sense in a scope on your model. (Of course a shortcut would be to simply eval '9..12' but evaling input from the end user is a really, really bad idea.)
Give a look at thinking sphinx(http://freelancing-god.github.com/ts/en/). It might make your task a lot easier. You can search in that:
http://freelancing-god.github.com/ts/en/searching.html#basic

Shortest way of determining a name ends with an `s`, `x` or `z`, and then use the `I18n.t` method with it

I'm creating a Rails application where users can have a first and last name. Since I'm a perfectionist, the application may not show something like Dennis's profile or Xianx's profile, but rather Dennis' profile and Xianx' profile. I use I18n, so I wanted to ask what is the shortest way of implementing this? This grammar is the same for both English and Dutch, where the application will be translated to.
Oh, some important things:
I am not afraid of using helpers and the application controller
My language files are in Ruby, not YAML
Thanks!
It's hard to be perfect in Dutch
def having_s( word ) # maybe genitiv_s is a better name
case word[-1,1] # [-1] will do in ruby 1.9
when 's', 'x', 'z'
"#{word}'"
else
"#{word}'s"
end
end
names=%w(Alex Inez Kees Maria Bordeaux)
names.each{|name| puts having_s(name)}
The last testcase ("Bordeaux") yields a wrong result, according to this.
One way to implement this with little risk of not being correct:
Instead of:
Person X's profile
Do:
Profile: Person X
There's not going to be a way to do the first for all languages/countries/cultures. The 2nd option may not look ideal, but you won't ever present anything incorrectly.

Resources