Rails Evaluate Integer as Boolean [closed] - ruby

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a model which stores user settings as 0 or 1 (for true/false). In my code, I'm having to do this:
if #user.settings.show_menu == 1
# do this
end
How can I leave out the == 1 or == 0? I've tried:
if #user.settings.show_menu
# do this
end
But it's not evaluating as true, same with when using !#user.settings.show_menu
It's been a long day, please guide me in the right direction. Thanks!

In Rails there is a boolean column for use with the database that stores as a number and converts accordingly. Generally this is encoded in the database as SMALLINT. If you have a regular INT you could always migrate to convert them.
An example migration:
change_table :table_name do |t|
t.change(:boolean_column, :boolean)
end
Within your app, the standard practice is to refer to boolean flags with their ? method version, like:
if (#user.settings.show_menu?)
# ...
end
If this method is not defined, you'll get an exception which can lead you to discover the problem. This compares favorably to having it always evaluate as true.

Related

Method Call with Ruby [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have got a exercise for Ruby, and don't know how to make it! I'm still a newb, and learning my way in programming world, but I can't resolve this exercise. If you could also explain to me how you made it, that'd be great!
Here it is :
Write a method double on object account which returns the double of its input parameter num.
def account.double(num)
#your code here
end
# call double here
def account.double(num)
num*2
end
account.double(54)
In ruby, methods return value of the last statement if return value is not specified explicitly. Here num*2 is returned by the method.
We are calling account.double at the end of the program using 54 as the number. You can use any other number you like.
Just remember that the account object should be created, before this double method can be defined/called. I am leaving that to you as an exercise.

Is there a way to use linq query with DateTime and a string working together in the same request? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying do a linq query where DateTime() is one of the requirements. It looks like this:
viewModel.MyModels = db.MyModels.Where(cd => cd.Date == Convert.ToDateTime(theDate).Where(cd => cd.itemOne == 8).Include(i => i.AnotherModel);
This works with a string but when I tried doing Convert.ToDateTime(theDate) I get a message that says DateTime does not have a definition for Where.
Is there another way that I can get the same result without having to convert all of my DateTime values to string?
Missing a paren?
viewModel.MyModels = db.MyModels.Where(cd => cd.Date == Convert.ToDateTime(theDate)).Where(cd => cd.itemOne == 8).Include(i => i.AnotherModel);

How do I execute a ruby script with a method but no class? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have:
def is_valid_cool_string(str)
...
end
And I want to write something like is_valid_cool_string("Foobar") at the bottom of the file to do this. Is there like a main method or something like that?
Thanks!
You don't need anything to make this work. :)
Don't forget to print the result if the method returns something.
puts is_valid_cool_string("Foobar")
or something like
puts "Is valid: #{is_valid_cool_string("Foobar").inspect}"

Creating model methods [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to create a method which will give me the user's full name.
the user has a first_name and a sur_name attribute.
I am trying to do something like
def fullname
"#{self.first_name} #{self.sure_name}"
end
but it gives me a method missing error when I try to do #user.fullname
am I missing something?
You have a typo: your method calls sure name (with an "e"), and you want sur name (without an "e").

Constructing Ruby's regular expression [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need some guidance on how to do the following..
Let say I have this string below
s= {"name":testName,"age":20}
how should I construct my regular expression in ruby, such that I am able to get "testName"
Thank you in advance
The data you have is a JSON string. You should not parse it with a regular expression, but parse it into an object and then access the name property.
I am not a Ruby dev, but you can do so if you have the JSON gem.
I agree with Jason about using JSON, but if you really can't...
1.9.3p194 > s= '{"name":testName,"age":20}'
=> "{\"name\":testName,\"age\":20}"
1.9.3p194 > s =~ /"name":(.*?)(,"|})/
=> 1
1.9.3p194 > puts $1
testName
Note that this is evil and wrong and will probably hurt little kittens... use JSON to save the kittens.

Resources