So I wanted to improve the UI of a feature that I was building by having the alert include the actual name of what I modifying and at what time. There is a specific syntax used to insert it and it looks like this within the control structure.
if var.updated_at
show.now[:alert] = "Your client, #{var.name} came at #{var.modified_time}."
end
These are my personal notes that I am making on my journey through computer science. It's just a great way for me to organize my thoughts and just maybe, I'll help someone else along the way. If this was any help, leave a comment.
Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts
Thursday, May 20, 2010
Wednesday, May 19, 2010
Ruby: if elsif elsif else statements... ya... they do exist.
I'm not crazy... just a n00b.
------------------------------------
# if, elsif and else illustrated
print "What distance in miles? "
miles = STDIN.gets.to_f
if miles < 2
puts "walk"
elsif miles < 6
puts "cycle"
elsif miles < 30
puts "bus or drive"
elsif miles < 350
puts "train"
else
puts "don't bother"
end
print "#{miles} miles is #{miles/5*8} kilometres\n"
Link for more info.
------------------------------------
# if, elsif and else illustrated
print "What distance in miles? "
miles = STDIN.gets.to_f
if miles < 2
puts "walk"
elsif miles < 6
puts "cycle"
elsif miles < 30
puts "bus or drive"
elsif miles < 350
puts "train"
else
puts "don't bother"
end
print "#{miles} miles is #{miles/5*8} kilometres\n"
Link for more info.
Tuesday, May 18, 2010
Ruby: String Manipulators
So I'm working on a feature that requires the input of a text field to be formatted in uppercase and have all spaces and dashes removed from the string.
So in the RAILS console, try:
>> a="Abs-a23-pdk 23 aB1"
=>"Abs-a23-pdk 23 aB1"
>> a.gsub!(" ", "")
=>"Abs-a23-pdk23aB1"
>> a.gsub!("-", "")
=> "Absa23pdk23aB1"
>> a.upcase!
=> "ABSA23PDK23AB1"
>> a
=> "ABSA23PDK23AB1"
Notes:
You need the "!" after the command to modify (make changes permanent) the receiver.
If you want to be fancy and write one line to do the 3 methods, try:
>> a="Abs-a23-pdk 23 aB1"
=> "Abs-a23-pdk 23 aB1"
>> a.gsub!(/[ -]/, "").upcase!
=> "ABSA23PDK23AB1"
>> a
=> "ABSA23PDK23AB1"
Notes:
So pretty much everything in between the brackets is removed is removed
[%$ _-] in this case, %, $, _, - and spaces are removed.
[1-9] in this case, all numbers are removed.
Here a link to the official documentation:
http://ruby-doc.org/core/classes/String.html
Another Link:
http://www.techotopia.com/index.php/Ruby_String_Replacement,_Substitution_and_Insertion
So in the RAILS console, try:
>> a="Abs-a23-pdk 23 aB1"
=>"Abs-a23-pdk 23 aB1"
>> a.gsub!(" ", "")
=>"Abs-a23-pdk23aB1"
>> a.gsub!("-", "")
=> "Absa23pdk23aB1"
>> a.upcase!
=> "ABSA23PDK23AB1"
>> a
=> "ABSA23PDK23AB1"
Notes:
You need the "!" after the command to modify (make changes permanent) the receiver.
If you want to be fancy and write one line to do the 3 methods, try:
>> a="Abs-a23-pdk 23 aB1"
=> "Abs-a23-pdk 23 aB1"
>> a.gsub!(/[ -]/, "").upcase!
=> "ABSA23PDK23AB1"
>> a
=> "ABSA23PDK23AB1"
Notes:
So pretty much everything in between the brackets is removed is removed
[%$ _-] in this case, %, $, _, - and spaces are removed.
[1-9] in this case, all numbers are removed.
Here a link to the official documentation:
http://ruby-doc.org/core/classes/String.html
Another Link:
http://www.techotopia.com/index.php/Ruby_String_Replacement,_Substitution_and_Insertion
Subscribe to:
Posts (Atom)