Tagged Questions
1
vote
3answers
76 views
How does == (equals) work under the hood in Ruby 1.8?
The following describes what seems to be a bug in Ruby 1.8 (and REE) but has been fixed in 1.9.
I'd like to know:
Why calling == on String or Fixnum triggers calling == on other object at all?
Why ...
16
votes
7answers
11k views
Test if string is a number in Ruby on Rails
I have this in my application controller
def is_number?(object)
true if Float(object) rescue false
end
and the following test in my controller
if mystring.is_number?
end
It's throwing an ...
0
votes
2answers
242 views
TypeError: wrong argument type String (expected Module) when trying to use rspec
Checking containment of a substring in a string.
I think I'm using the right syntax as documented here, but it's not working for me. What am I missing?
>> require 'rspec-expectations'
=> ...
0
votes
3answers
78 views
How can I check if a string has multiple characters in Ruby?
I want to make sure a string has the format of an ip address. I don't need anything too specific. I just want to check if there are three . in the string. I can check if there is one . in there ...
39
votes
6answers
28k views
Ruby on Rails: how to render a string as HTML?
I have
@str = "<b>Hi</b>"
and in my erb view:
<%= @str >
What will display on the page is: <b>Hi</b> when what I really want is Hi. What's the ruby way to ...
0
votes
2answers
28 views
Custom regular expression i18n
I'm using Rails 3.2.
I'm localizing my site in Romanian. In regular expressions, the regexp interval [a-z] should contain, in order, the following letters:
a, ă, â, b, c etc.
Is there a way to tell ...
0
votes
1answer
22 views
Ruby, Convert String Queries Into Method Calls
The code below works completely fine as long as users enter in the method name. I'd like to avoid requiring users to enter the name of the method at the various gets.chomp prompts.
I thought that ...
2
votes
1answer
49 views
How do I do string indexing using negative to positive indexes?
For a given string:
string = "hello world"
I want to return a string using indexing:
string2 = string[-4..2]
string2 = "orldhel"
Why does ruby return:
string2 = ""
PS - I will be iterating ...
1
vote
1answer
45 views
How to convert a string to a hash in ruby/rails without using eval? [duplicate]
Here is the string which needs to convert into a hash.
"{:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }"
We can not use eval because eval ...
4
votes
3answers
63 views
Splitting a String in Multiple points using Ruby on Rails
I have a string in my DB that represents notes for a user. I want to split this string up so I can separate each note into the content, user, and date.
Here is the format of the String:
...
0
votes
3answers
57 views
Add three string and merge by “-” ruby
I have three strings:
first = "test"
second = "hello"
third = "world"
I want to concatenate them like this:
test-hello-world
I tried using +:
first + "-" + second + "-" + third
But I'm ...
3
votes
1answer
64 views
Ruby Strings - difference between << and +=
Running the method fizzbuzz1 yields a 100 member list of numbers 1 to 100, where each multiple of 3 is replaced by "fizz", each multiple of 5 by "buzz", and each multiple of both 3 and 5 by ...
4
votes
3answers
3k views
Ruby: counting digits in a float number
Is there any worthy Ruby method to count the number of digits in a float? Also, how do I specify the precise when to_s float numbers?
2
votes
2answers
925 views
How to use string in Ruby variable name?
I have a string that I want to use as part of a variable name. Specifically, in Rails route paths:
<%= foo_path %>
<%= bar_path %>
I want the first part of the route name to be dynamic. ...
3
votes
4answers
55 views
Ruby - Comparing “==” hex value to string
I am basically reading in the header of a picture file and doing a quick comparison to see what kind of file it actually is. BMP, GIF, PNG are all easy as their headers contain BM, GIF, and PNG ...